In this article, we will discuss how to split strings using specified delimiter in String’s split() method
1. String’s split() method:
- This String method is used to split strings using specified delimiter (i.e.; using regex pattern) and
- returns string[] array
Note:
- there are 2 variants or overloaded split() methods
- 1st variant by default split string using specified delimiter
- Whereas 2nd variant split string using specified delimiter but with threshold limit specified, as other argument (2nd argument)
1.1 Method Signature:
public String[] split(String regex);
public String[] split(String regex, int limit);
1.2 Parameters:
- regex –> regular expression which is used as delimiter to split string
- limit –> in the 2nd variant, we can specify threshold limit i.e.; in how many maximum parts string should be split-ed
1.3 Returns:
split() method |
Returns |
public String[] split(String regex); | Returns string[] arrays after splitting the invoking string using specified delimiter (or regex) |
public String[] split(String regex, int limit); | Returns string[] arrays after splitting the invoking string using specified delimiter (or regex) with specified threshold limit |
1.4 Throws:
- PatternSyntaxException, if the specified regular expression syntax is invalid (or simply not compliable)
2. Examples on split() method:
2.1 Java program to split strings (with 3 different possible delimiter)
The delimiter are,
- space (” “)
- Dot or period (“.”)
- forward slash (“/”)
Method Signature:
public String[] split(String regex);
StringSplitMethod.java
package in.bench.resources.string.methods;
public class StringSplitMethod {
public static void main(String[] args) {
// Example 1: test string
String testStr1 = "English is a easy language "
+ "but Java is super cool language";
// split using space (" ")
String[] splittedStrArrays1 = testStr1.split(" ");
// print to console
System.out.println("Example 1 : split(\" \") "
+ "using space\n");
System.out.println("Original string : " + testStr1);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str1 : splittedStrArrays1) {
System.out.println(str1);
}
// Example 2: test string
String testStr2 = "www.benchresources.net";
// split using DOT or period (".")
String[] splittedStrArrays2 = testStr2.split("\\.");
// print to console
System.out.println("\n\nExample 2 : split(\".\") "
+ "using DOT or period\n");
System.out.println("Original string : " + testStr2);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str2 : splittedStrArrays2) {
System.out.println(str2);
}
// Example 3: test string
String testStr3 = "30/07/2017";
// split using DOT or period (".")
String[] splittedStrArrays3 = testStr3.split("/");
// print to console
System.out.println("\n\nExample 3 : split(\"/\") "
+ "using forward-slash\n");
System.out.println("Original string : " + testStr3);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str3 : splittedStrArrays3) {
System.out.println(str3);
}
}
}
Output:
Example 1 : split(" ") using space
Original string : English is a easy language but Java is super cool language
Splitted string :
English
is
a
easy
language
but
Java
is
super
cool
language
Example 2 : split(".") using DOT or period
Original string : www.benchresources.net
Splitted string :
www
benchresources
net
Example 3 : split("/") using forward-slash
Original string : 30/07/2017
Splitted string :
30
07
2017
2.2 Java program to split string with specified threshold limit (with 3 different possible delimiter)
The delimiter are,
- space (” “)
- Dot or period (“.”)
- forward slash (“/”)
Method Signature:
public String[] split(String regex, int limit);
StringSplitMethod2.java
package in.bench.resources.string.methods;
public class StringSplitMethod2 {
public static void main(String[] args) {
// Example 1: test string
String testStr1 = "English is a easy language "
+ "but Java is super cool language";
// split using space (" ") - with threshold-limit 7
String[] splittedStrArrays1 = testStr1.split("\\s", 7);
// print to console
System.out.println("Example 1 : split(\" \") using space"
+ " - with threshold-limit 7\n");
System.out.println("Original string : " + testStr1);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str1 : splittedStrArrays1) {
System.out.println(str1);
}
// Example 2: test string
String testStr2 = "www.benchresources.net";
// split using DOT or period (".")
// with threshold-limit 2
String[] splittedStrArrays2 = testStr2.split("\\.", 2);
// print to console
System.out.println("\n\nExample 2 : split(\".\") "
+ "using DOT or period"
+ " - with threshold-limit 2\n");
System.out.println("Original string : " + testStr2);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str2 : splittedStrArrays2) {
System.out.println(str2);
}
// Example 3: test string
String testStr3 = "30/07/2017";
// split using DOT or period (".")
// with threshold-limit 2
String[] splittedStrArrays3 = testStr3.split("/", 2);
// print to console
System.out.println("\n\nExample 3 : split(\"/\") "
+ "using forward-slash"
+ " - with threshold-limit 2\n");
System.out.println("Original string : " + testStr3);
System.out.println("\nSplitted string : ");
// iterating using enhanced for-loop
for(String str3 : splittedStrArrays3) {
System.out.println(str3);
}
}
}
Output:
Example 1 : split(" ") using space - with threshold-limit 7
Original string : English is a easy language but Java is super cool language
Splitted string :
English
is
a
easy
language
but
Java is super cool language
Example 2 : split(".") using DOT or period - with threshold-limit 2
Original string : www.benchresources.net
Splitted string :
www
benchresources.net
Example 3 : split("/") using forward-slash - with threshold-limit 2
Original string : 30/07/2017
Splitted string :
30
07/2017
Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!