The Java String split method is an extremely useful tool for dividing a string up into smaller substrings based on some delimiter or regular expression. You can use this method to quickly and easily parse out pieces of information.
In this article, we will provide an example of how to write a String split function. The syntax for the String split function in Java Programming is shown below.
Post On: | split() Method Of String in Java |
Post Type: | Java Tutorials |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | SoftwareTestingo Telegram Group |
Java String split() Syntax
This Java function can be used to split a string into an array of substrings, based on the separator. The parameter for this function can be a regular expression, which allows you to specify how the original string should be divided up. This is useful if you need to break apart a string that has multiple instances of the same character or substring.
We have two different versions of the split() method in the String class.
1. public String[] split(String regex)
The java string split() method is used to split the given string based on a regular expression. The returned array will include the entire original string, Split into substrings based on the regular expression provided. This function was introduced in Java 1.4 and any trailing empty strings are not included as part of the output array.
Syntax:
public String[] split(String regex); //In order to use in program String_Object.split(String regex);
This Java split function accepts one argument, which is a regular expression that will be used as the separator.
Parameter (regex) – The delimiting regular expression
Returns: The split() method will return an array of strings that are computed by splitting the given string.
PatternSyntaxException Exception: If you see a PatternSyntaxException, it means that there is something wrong with the regular expression you are using. Check to make sure that your syntax is correct.
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx { public static void main(String[] args) { String str="Welcome to SoftwareTestingo Blog"; String arr[]=str.split("["); for (String a : arr) System.out.println(a); } }
NullPointerException: If you pass null in the split() method then you will get NullPointerException.
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx2 { public static void main(String[] args) { String str="Welcome to SoftwareTestingo Blog"; String arr[]=str.split(null); for (String a : arr) System.out.println(a); } }
Using Delimeter: If you want to split a string based on a given token, you can use the Java program below. In the given example, the string is split for the delimiter colon”:”.
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx3 { public static void main(String[] args) { String str="Learning:Welcome to SoftwareTestingo Blog"; String arr[]=str.split(":"); for (String a : arr) System.out.println(a); } }
With White Space: We can also split the string based on the white spaces for this we can use ” ” or “\\s”
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx4 { public static void main(String[] args) { String str="Welcome to SoftwareTestingo Blog"; String arr[]=str.split(" "); for (String a : arr) System.out.println(a); } }
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx4 { public static void main(String[] args) { String str="Welcome to SoftwareTestingo Blog"; String arr[]=str.split("\\s"); for (String a : arr) System.out.println(a); } }
Split String by Multiple Delimiters: This Java program demonstrates how to split a string with multiple delimiters using the OR (|) operator. The example uses two delimiters, white space, and a dot.
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx5 { public static void main(String[] args) { String str="Learn.Welcome to SoftwareTestingo Blog"; String arr[]=str.split("\\.|\\s"); for (String a : arr) System.out.println(a); } }
Using String: We can split by using String.
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx6 { public static void main(String[] args) { String str="Welcome to SoftwareTestingo Blog"; String arr[]=str.split("to"); for (String a : arr) System.out.println(a); } }
2. Public 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 other methods is that it limits the number of strings returned after a string is split up.
Parameter (regex, limit) – The delimiting regular expression(regex) & the maximum number of tokens threshold.
For the limit parameter we can have the 3 values:
- limit > 0 – This means that the pattern will be applied a maximum of limit-1 times, the resulting array will be no longer than n, and the final element in the array will contain everything after the last matching pattern.
- limit < 0 – The pattern will be applied as many times as possible and the resulting array can be of any size.
- limit = 0 – The pattern will be applied as many times as possible until it can’t be applied anymore. The resulting array can be of any size, and trailing empty strings will be discarded.
Returns: The split() method will return an array of strings that are computed by splitting the given string.
Example 1:
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx7 { public static void main(String[] args) { String str="Welcome@to@SoftwareTestingo@Blog "; String arr[]=str.split("@", 2); for (String a : arr) System.out.println(a); } }
Example 2:
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx7 { public static void main(String[] args) { String str="Welcome@to@SoftwareTestingo@Blog "; String arr[]=str.split("@", -2); for (String a : arr) System.out.println(a); } }
Example 3:
package com.SoftwareTestingO.Java.basics; public class SplitPatternSyntaxExceptionEx7 { public static void main(String[] args) { String str="Welcome@to@SoftwareTestingo@Blog "; String arr[]=str.split("@", 0); for (String a : arr) System.out.println(a); } }
Conclusion:
The Java String class’ split() method is a very useful tool that’s often used. Most data, especially data obtained from reading files, requires some amount of pre-processing, such as splitting the string, to get meaningful information from it.
Leave a Reply