Mar 052018

ADM

How to split a string in Java

  • 05 March 2018
  • ADM

 

 

 

How to split a string in Java - images/logos/java.jpg

 

To split a string in Java the split() method can be used.

There are the two variants of split() method in Java:

  • public String[] split(String regex), the limit parameter, in this case, is 0.
  • public String[] split(String regex, int limit)

The split methods split this string around matches of the given regular expression.

The limit parameter controls the number of times the pattern is applied and affects the length of the resulting array.

  • limit n > 0 then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
  • limit n < 0 then the pattern will be applied as many times as possible and the array can have any length.
  • limit n = 0 then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

split(String regex)

package com.admfactory;

public class SplitStringExample1 {

	public static void main(String[] args) {
		System.out.println("Java Split Example");
		System.out.println();
		String s = "aaa@bbb@ccc@@ddd@eee@fff@@@";
		
		System.out.println("String: " + s);
		System.out.println();
		
		String[] split = s.split("@");
		
		System.out.println("Result: ");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);	
		}
		
	}
}

Output

Java Split Example

String: aaa@bbb@ccc@@ddd@eee@fff@@@

Result: 
aaa
bbb
ccc

ddd
eee

From the output can see the empty token between ccc and ddd is included, but the empty trailing tokens not.

split(String regex, int limit)

Example 1

package com.admfactory;

public class SplitStringExample2 {

	public static void main(String[] args) {
		System.out.println("Java Split Example");
		System.out.println();
		String s = "aaa@bbb@ccc@@ddd@eee@fff@@@";
		
		System.out.println("String: " + s);
		System.out.println();
		
		String[] split = s.split("@", 3);
		
		System.out.println("Result: ");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);	
		}
		
	}
}

Output

Java Split Example

String: aaa@bbb@ccc@@ddd@eee@fff@@@

Result: 
aaa
bbb
ccc@@ddd@eee@fff@@@

From the output can be seen that the split was applied three times and the rest of the string was dumped into the last element from the array.

Example 2

package com.admfactory;

public class SplitStringExample2 {

	public static void main(String[] args) {
		System.out.println("Java Split Example");
		System.out.println();
		String s = "aaa@bbb@ccc@@ddd@eee@fff@@@";
		
		System.out.println("String: " + s);
		System.out.println();
		
		String[] split = s.split("@", 15);
		
		System.out.println("Result: ");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);	
		}
		
	}
}

Output

Java Split Example

String: aaa@bbb@ccc@@ddd@eee@fff@@@

Result: 
aaa
bbb
ccc

ddd
eee
fff

From the output can be seen that contains 10 elements, all from the array.

Example 3

package com.admfactory;

public class SplitStringExample2 {

	public static void main(String[] args) {
		System.out.println("Java Split Example");
		System.out.println();
		String s = "aaa@bbb@ccc@@ddd@eee@fff@@@";
		
		System.out.println("String: " + s);
		System.out.println();
		
		String[] split = s.split("@", -1);
		
		System.out.println("Result: ");
		for (int i = 0; i < split.length; i++) {
			System.out.println(split[i]);	
		}
		
	}
}

Output

Java Split Example

String: aaa@bbb@ccc@@ddd@eee@fff@@@

Result: 
aaa
bbb
ccc

ddd
eee
fff

 

 

 

References