[ Web Proxy ]
URL:
Viewing: https://www.tutorialspoint.com/java/lang/string_split.htm [Back]  [Original]

Java - String split() Method
Tutorials Courses Jobs Login
Home Java/lang Java - String split() Method

Java - String split() Method



The Java String split() method is used to divide/split a string into an array of substrings. This method accepts a string representing a regular expression as a parameter, searches for the given pattern in the current string and, splits it at every match.

Each substring of the string that is terminated by the substring that matches the given regular expression is included in the resultant array. If the expression does not match any part of the given string, then the resulting array has just one element, which is the given string.

The way this method operates is the same as calling split() method with the given expression and a limit argument of zero. Therefore, the resulting array does not contain trailing empty strings.

This method has two polymorphic variants. The syntaxes are shown below.

Syntax

Following is the syntax for Java String split() method

public String[] split(String regex) // first syntax
or,
public String[] split(String regex, int limit) // second syntax

Parameters

  • regex This is the delimiting regular expression.

  • limit This controls the number of times the pattern is applied and therefore affects the length of the resulting array // second syntax

Return Value

This method returns the array of strings computed by splitting the given string around matches of the given regular expression.

Splitting a String based on delimiter patterns Example

The following example shows the usage of Java String split() method by splitting the String object and counting the tokens by providing a limit '3' to the string

package com.tutorialspoint;

public class StringDemo { 
   public static void main(String[] args) {
      String str = "a d, m, i.n";
      String delimiters = "\\s+|,\\s*|\\.\\s*";
      
      // analyzing the string
      String[] tokensVal = str.split(delimiters); 
      
      // prints the count of tokens
      System.out.println("Count of tokens = " + tokensVal.length);    
      for(String token : tokensVal) {
         System.out.print(token);
      }     
      
      // analyzing the string with limit as 3
      tokensVal = str.split(delimiters, 3); 
      
      // prints the count of tokens 
      System.out.println("\nCount of tokens = " + tokensVal.length);    
      for(String token : tokensVal) {
         System.out.print(token);
      }    
   }
}

Output

If you compile and run the above program, it will produce the following result

Count of tokens = 5
admin
Count of tokens = 3
adm, i.n

Splitting a String based on delimiter patterns Example

In the following example split() method is used to split the String object and count the tokens without providing any limit to the string

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "a d, m, i.n";
      String delimiters = "\\s+|,\\s*|\\.\\s*";
      
      // analyzing the string 
      String[] tokensVal = str.split(delimiters); 
      
      // prints the number of tokens
      System.out.println("Count of tokens = " + tokensVal.length);    
      for(String token : tokensVal) {
         System.out.print(token);
      } 
   }
}

Output

If you compile and run the program above, the output will be displayed as follows

Count of tokens = 5
admin

Splitting a String based on delimiter String Example

In this example, we are creating a Java String object with the value "WelcomeLaughtoLaughtutorialsPoint" and we are trying to split this string each time we encounter the substring "laugh".

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String s = "WelcomeLaughtoLaughtutorialsPoint";
      String[] splitting = s.split("Laugh");
      for (String splt : splitting) {
         System.out.println(splt);
      }
   }
}

Output

On executing the program above, the output is obtained as follows

Welcome
to
tutorialsPoint

Splitting a String based on delimiter patterns Example

In this case, we are a initializing Java String with a single word and trying to split it using the character in the String.

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome";
      String[] splitting = s.split("l");
      
      // splitting the string on 'l' using the regex
      for (int i = 0; i < splitting.length; i++) {
         System.out.println(splitting[i]);
      }
   }
}

Output

The output of the above program is as follows

We
come

Checking Exception while Spliting String based on an Invalid Patterns Example

The delimiter regular expression will throw a PatternSyntaxException if the syntax is invalid. Take a look at the example below

public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome to tutorials***Point";
      String[] splitting = s.split("***");
      
      // splitting the string on 'l' using the regex
      for (String text : splitting) {
         System.out.println(text);
      }
   }
}

PatternSyntaxException

While executing the above code we get the following output

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
***
^
      at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
      at java.base/java.util.regex.Pattern.sequence(Pattern.java:2213)
      at java.base/java.util.regex.Pattern.expr(Pattern.java:2079)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1793)
      at java.base/java.util.regex.Pattern.(Pattern.java:1440)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
      at java.base/java.lang.String.split(String.java:3149)
      at java.base/java.lang.String.split(String.java:3195)
      at StringDemo.main(StringDemo.java:4)
java_lang_string.htm
Tutorix - AI Tutor [Tutorix - AI Tutor]
Print Page
Advertisements

Web Proxy Viewer  |  New URL  |  Original Page