Software >> Development >> Languages >> Java >> Examples >>  How to split a string

/* * Uses split to break up a string of input separated by * commas and/or whitespace. */ import java.util.regex.*; public class RegexSplit { public static void main(String[] args) throws Exception { // Create a pattern to match breaks String buf = "one,two,three"; Pattern p = Pattern.compile("[,]"); System.out.println("Before split = " buf); // Split input with the pattern String[] result = p.split(buf); for (int i=0; i<result.length; i ) System.out.println(result[i]); } }