w3resource

Java String: split() Method

public String[] split(String regex)

The split() method is used to split a given string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

Java Platform: Java SE 8

Syntax:

split(String regex)

Parameters:

Name Description Type
regex the delimiting regular expression String

Return Value: the array of strings computed by splitting this string around matches of the given regular expression.

Return Value Type: String

Throws:
PatternSyntaxException - if the regular expression's syntax is invalid.

Pictorial presentation of Java String split() Method

Java String: split() Method

Example: Java String split(String regex) Method

The following example shows the usage of java String() method.

public class StringSplitRegex {

	public static void main(String[] args) {
		// Declare string object
		String StringValue = "John Gilbert:43:UK";
		// define the regex parameter
		String regex = ":";
		// split the string object
		String[]output = StringValue.split(regex);
		System.out.println();
		// printing the array contents
		System.out.println("Name:"+output[0]);
		System.out.println("Age:"+output[1]);
		System.out.println("Country:"+output[2]);
        System.out.println();
	}

}

Output:

Name:John Gilbert                                      
Age:43                                                 
Country:UK

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

Splits this string around matches of the given regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

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 is greater than zero 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 is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The string "boo:and:foo", for example, yields the following results with these parameters:

Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }

An invocation of this method of the form str.split(regex, n) yields the same result as the expression

Pattern.compile(regex).split(str, n)

Java Platform: Java SE 8

Syntax:

split(String regex, int limit)

Parameters:

Name Description Type
regex the delimiting regular expression String
limit the resulting threshold, as described above int

Return Value : the array of strings computed by splitting this string around matches of the given regular expression.

Return Value Type:

Throws:
PatternSyntaxException - if the regular expression's syntax is invalid

Example: Java String startsWith(String prefix int limit) Method

The following example shows the usage of java String() method.

public class SplitExample {
   public static void main(String args[]){
       String str = new String("28/12/1972");
       System.out.println();
       System.out.println("split(String regex):");
       String array1[]= str.split("/");
       for (String abc: array1){
          System.out.println(abc);
       }
       System.out.println("split(String regex, int limit) with limit=2:");
       String array2[]= str.split("/", 2);
       for (String abc: array2){
          System.out.println(abc);
       }
       System.out.println("split(String regex, int limit) with limit=0:");
       String array3[]= str.split("/", 0);
       for (String abc: array3){
          System.out.println(abc);
       }
       System.out.println("split(String regex, int limit) with limit=-5:");
       String array4[]= str.split("/", -5);
       for (String abc: array4){
          System.out.println(abc);
          System.out.println();
       }
   }
}

Output:

java SplitExample                                      
                                                       
split(String regex):                                   
28                                                     
12                                                     
1972                                                   
split(String regex, int limit) with limit=2:           
28                                                     
12/1972                                                
split(String regex, int limit) with limit=0:           
28                                                     
12                                                     
1972                                                   
split(String regex, int limit) with limit=-5:          
28                                                     
                                                       
12                                                     
                                                       
1972

Java Code Editor:

Previous:replaceFirst Method
Next:startsWith Method



Follow us on Facebook and Twitter for latest update.