Split the string in java is important frequency asked questions . Because In our application we need to spit string many times . In this tutorial we see examples of split method in java . String split function in java have two form as
public String split(String regex);
public String split(String regex, int limit);
We will discuss about both form of split method in java step by step
The split() Method Example (Without a Limit)
This method take argument string as regex format. This method splits this string around matches of the given regular expression.
Syntax :
public String split(String regex);
Parameters: regex the delimiting regular expression
Returns: It return the array of strings computed by splitting this string around matches of the given regular. Expression : Its Throws PatternSyntaxException if the regular expression’s syntax is invalid
Now let’s see How to split string by comma In Java . In this example we will use comma as delimiter.
public class StringSplitExample {
public static void main(String[] args) {
String str = "India,America,China,England";
String[] namesOfArray = str.split(",");
System.out.println("Names are: ");
for (String name : namesOfArray) {
System.out.println(name);
}
}
}
Output :
Names are:
India
America
China
England
In this example we have learned how to split a string into multiple strings in java . Let’s see more example of java split() method.
How to split string by space In Java
In this example we will split a string in java with space delimiter.
public class StringSplitExample {
public static void main(String[] args) {
String str = "India America China England";
String[] namesOfArray = str.split(" ");
System.out.println("Names are: ");
for (String name : namesOfArray) {
System.out.println(name);
}
}
}
Output:
Names are:
India
America
China
England
How to Split string by dot In Java
In this example we will split string by dot(.) in Java . Because dot is special character we need escape it using \ and in java backslash also require \ so we will use \\ double backslash. Alternatively, we can also use [.] because only ]^-\ characters have a special meaning inside of character classes.
public class StringSplitExample {
public static void main(String[] args) {
String str = "India.America.China.England";
String[] namesOfArray = str.split("\\.");
System.out.println("Names are: ");
for (String name : namesOfArray) {
System.out.println(name);
}
String[] namesOfArray1 = str.split("[.]");
System.out.println("Names are from [.]: ");
for (String name : namesOfArray1) {
System.out.println(name);
}
}
}
Output :
Names are:
India
America
China
England
Names are from [.]:
India
America
China
England
In this example we have explained how to split string by dot in java .
Java split() Method Examples (With a Limit)
Splits this string on base matches of the given regular expression and limit parameter.
Syntax :
public String split(String regex, int limit);
regex we have discussed above Now we will discuss about limit . There are three cases for limit .
- limit > 0 – In this is the case , the pattern will be applied at most limit-1 times, the resulting array’s length will not be more than n, and the resulting array’s last entry will contain all input beyond the last matched pattern.
- limit < 0 – In this case, the pattern will be applied as many times as possible, and the resulting
array can be of any size. - limit = 0 – In this case, the pattern will be applied as many times as possible, the resulting array can
be of any size, and trailing empty strings will be discarded.
How limit work Let’s splitting String java@vogue@exampleaa
Regex Limit result
@ 2 {“java” ,”vogue@exampleaa”}
@ 5 {“java” ,”vogue”,”exampleaa”}
@ -1 {“java” ,”vogue”,”exampleaa”}
a 6 {“j” ,”v”,”@vogue@ex”,”mple”,””,””}
a -1 {“j” ,”v”,”@vogue@ex”,”mple”,””,””}
a 0 {“j” ,”v”,”@vogue@ex”,”mple”}
Example 1 :
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("@" ,2);
for (String element : arr) {
System.out.println(element);
}
}
}
Output :
java
vogue@exampleaa
Example 2:
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("@" ,5);
for (String element : arr) {
System.out.println(element);
}
}
}
Output :
java
vogue
exampleaa
Example 3:
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("@" ,-1);
for (String element : arr) {
System.out.println(element);
}
}
}
Output:
java
vogue
exampleaa
Example 4:
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("a" ,6);
for (String element : arr) {
System.out.println(element);
}
System.out.println("==>>Done ==>>");
}
}
Output :
j
v
@vogue@ex
mple
==>>Done ==>>
Example 5:
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("a" ,-1);
for (String element : arr) {
System.out.println(element);
}
System.out.println("==>>Done ==>>");
}
}
Output :
j
v
@vogue@ex
mple
==>>Done ==>>
Example 6:
public class StringSplitExampleWithLimit {
public static void main(String[] args) {
String str="java@vogue@exampleaa";
String[] arr = str.split("a" ,0);
for (String element : arr) {
System.out.println(element);
}
System.out.println("==>>Done ==>>");
}
}
Output :
j
v
@vogue@ex
mple
==>>Done ==>>
In this tutorials we split the string in java using split() method in different ways. Now we can split a string in java with delimiter easily . If you are new in java , then i recommenced to you See Java Tutorial for beginners . If you are preparing Java coding Interview then should see Java String Interview Program list . We have also Java coding Interview Question collection . If you are looking Java Collection Framework Tutorials then i recommenced to you see Collection In Java with examples .