Splitting Strings

10 February 2012 By Nithya Vasudevan 415 views No Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

The split method splits the string around matches of the given regular expression and the matched strings are returned as an array of String objects. The method signature is as follows,

public String[] split(String regex)
public String[] split(String regex, int limit)

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

  • If the 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.
  • If n < 0, then the pattern will be applied as many times as possible and the array can have any length.
  • If 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.
package com.ibytecode.strings.methods;

public class StringSplit {

	public static void main(String[] args) {
		String str = "01/01/2010";
		// split on '/'
		System.out.println("Split on '/' " + str);
		String[] tokens = str.split("/");
		for (String token : tokens) {
			System.out.println(token);
		}
		System.out.println("---------------------------------------");
		
		// split on '/' with limit = 2 ( > 0)
		System.out.println("Split on '/' with limit = 2, " + str);
		tokens = str.split("/", 2);
		for (String token : tokens) {
			System.out.println(token);
		}
		System.out.println("---------------------------------------");
		
		// split on '0' with limit = -2 ( < 0)
		System.out.println("Split on '0' with limit = -2, " + str);
		tokens = str.split("0", -2);
		for (String token : tokens) {
			System.out.println("'" + token + "'");
		}
		System.out.println("---------------------------------------");
		
		// split on '0' with limit = 0
		System.out.println("Split on '0' with limit = 0, " + str);
		tokens = str.split("0", 0);
		for (String token : tokens) {
			System.out.println("'" + token + "'");
		}
		System.out.println("---------------------------------------");
	}
}

Split on ‘/’ 01/01/2010
01
01
2010
—————————————
Split on ‘/’ with limit = 2, 01/01/2010
01
01/2010
—————————————
Split on ’0′ with limit = -2, 01/01/2010

’1/’
’1/2′
’1′

—————————————
Split on ’0′ with limit = 0, 01/01/2010

’1/’
’1/2′
’1′
—————————————

Note that in the output with limit = 0, the trailing empty token is discarded.

java.util.StringTokenizer class also splits/tokenizes a string which is explained here. StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended to use the split method of String or the java.util.regex package instead.

Tags: , ,