how-to

Working with String

How to split string to parts

When you have string that you want to split, use function java.lang.String.Split(). The first parameter of this function is mask for spliting, you can use here advanced regular expressions too.

/* -- split by spaces */>

String s = "This is some text";

String[] array = s.split( " " );

System.out.println( s + " -> " + Arrays.toString( array ) );

/* -- split by " is" text */

String[] array1 = s.split( " is" );

System.out.println( s + " -> " + Arrays.toString( array1 ) );

/* -- split by all spaces of any length */

String s2 = "This is some     text";

String[] array2 = s2.split( "\\s+" );

System.out.println( s2 + " -> " + Arrays.toString( array2 ) );

/* -- split groups in () */

String s3 = "(123)(A)(ABDC)";

String[] array3 = s3.split( "[()]" );

System.out.println( s3 + " -> " + Arrays.toString( array3 ) );
The output is:
This is some text -> [This, is, some, text]
This is some text -> [This,  some text]
This is some     text -> [This, is, some, text]
(123)(A)(ABDC) -> [, 123, , A, , ABDC]