Java Split Example Regular Expression
Java String split method is used for splitting a String into its substrings based on the given delimiter or regular expression.
For example:
String: [email protected] Regular Expression: @ Output : {"chaitanya", "singh"}
Java String Split Method
We have two variants of split() method in String class.
1. String[] split(String regex)
: It returns an array of strings after splitting an input String based on the delimiting regular expression.
2. String[] split(String regex, int limit)
: This Java String split method is used when we want the substrings to be limited. The only difference between this method and above method is that it limits the number of strings returned after split up. For e.g. split("anydelimiter", 3)
would return the array of only 3 strings even if the delimiter is present in the string more than 3 times.
If the limit is negative then the returned array would be having as many substrings as possible however when the limit is zero then the returned array would be having all the substrings excluding the trailing empty Strings.
It throwsPatternSyntaxException
if the syntax of specified regular expression is not valid.
Java String split Example
public class SplitExample{ public static void main(String args[]){ // This is out input String String str = new String("28/12/2013"); System.out.println("split(String regex):"); /* Here we are using first variation of java string split method * which splits the string into substring based on the regular * expression, there is no limit on the substrings */ String array1[]= str.split("/"); for (String temp: array1){ System.out.println(temp); } /* Using second variation of split method here. Since the limit is passed * as 2. This method would only produce two substrings. */ System.out.println("split(String regex, int limit) with limit=2:"); String array2[]= str.split("/", 2); for (String temp: array2){ System.out.println(temp); } System.out.println("split(String regex, int limit) with limit=0:"); String array3[]= str.split("/", 0); for (String temp: array3){ System.out.println(temp); } /* When we pass limit as negative. The split method works same as the first variation * because negative limit says that the method returns substrings with no limit. */ System.out.println("split(String regex, int limit) with limit=-5:"); String array4[]= str.split("/", -5); for (String temp: array4){ System.out.println(temp); } } }
Output:
split(String regex): 28 12 2013 split(String regex, int limit) with limit=2: 28 12/2013 split(String regex, int limit) with limit=0: 28 12 2013 split(String regex, int limit) with limit=-5: 28 12 2013
Difference between zero and negative limit in java string split method
In the above example split("/",0) and split("/",-5) returned same value however in some cases the result would be different. Lets see the difference between these two with the help of an example:
String s="bbaaccaa"; String arr1[]= s.split("a", -1); String arr2[]= s.split("a", 0);
In this case arr1 would be having {"bb", " ", "cc", " ", " "} However arr2 would be having {"bb", " ", "cc"} because limit zero excludes trialing empty Strings.
Lets see the complete program.
public class JavaExample{ public static void main(String args[]){ // This is out input String String s = new String("bbaaccaa"); //Splitting with limit as 0 String arr2[]= s.split("a", 0); System.out.println("Zero Limit split:"); for (String str2: arr2){ System.out.println(str2); } //Splitting with negative limit String arr1[]= s.split("a", -1); System.out.println("Negative Limit split:"); for (String str: arr1){ System.out.println(str); } System.out.println("End of program"); } }
Output:
Java String split with multiple delimiters (special characters)
Lets see how we can pass multiple delimiters while using split() method. In this example we are splitting input string based on multiple special characters.
public class JavaExample{ public static void main(String args[]){ String s = " ,ab;gh,bc;pq#kk$bb"; String[] str = s.split("[,;#$]"); //Total how many substrings? The array length System.out.println("Number of substrings: "+str.length); for (int i=0; i < str.length; i++) { System.out.println("Str["+i+"]:"+str[i]); } } }
Output:
Lets practice few more examples:
Example: word as a regular expression in Java String split method
public class SplitExample1 { public static void main(String args[]) { String str = "helloxyzhixyzbye"; String[] arr = str.split("xyz"); for (String s : arr) System.out.println(s); } }
Output:
hello hi bye
Example: splitting string based on whitespace
public class SplitExample2 { public static void main(String args[]) { String str = "My name is Chaitanya"; //regular expression is a whitespace here String[] arr = str.split(" "); for (String s : arr) System.out.println(s); } }
Output:
My name is Chaitanya
vaughantherinchis.blogspot.com
Source: https://beginnersbook.com/2013/12/java-string-split-method-example/
0 Response to "Java Split Example Regular Expression"
Post a Comment