Split string into array in java

Split string into array in java
/**
 * @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));
		
	}
}
S‮ww:ecruo‬w.lautturi.com

output:

[root, :1:100:superuser]
[root, , 1, 100, superuser]

split Syntax

String[] java.lang.String.split(String regex, int limit)

Splits this string around matches of the given regular expression.

Parameters:

regex: the delimiting regular expression
limit: the result threshold

Created Time:2017-09-29 23:32:51  Author:lautturi