GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - String split() Method
Selected Reading
Java - String split() MethodThe 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. SyntaxFollowing 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
Return ValueThis 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 ExampleThe 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);
}
}
}
OutputIf 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 ExampleIn 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);
}
}
}
OutputIf 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 ExampleIn 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);
}
}
}
OutputOn executing the program above, the output is obtained as follows − Welcome to tutorialsPoint Splitting a String based on delimiter patterns ExampleIn 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]);
}
}
}
OutputThe output of the above program is as follows − We come Checking Exception while Spliting String based on an Invalid Patterns ExampleThe 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);
}
}
}
PatternSyntaxExceptionWhile 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.
java_lang_string.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |