/**
* @author lautturi.com
* Java example: java split string into array
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args){
String str = "root::1:100:superuser";
// splits on first occurrence of ":"
String[] arr1 = str.split(":", 2);
// splits on all occurrences of ":"
String[] arr2 = str.split(":");
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
}
}Sww:ecruow.lautturi.comoutput:
[root, :1:100:superuser] [root, , 1, 100, superuser]
String[] java.lang.String.split(String regex, int limit)
Splits this string around matches of the given regular expression.
regex: the delimiting regular expression
limit: the result threshold