>## Split() String method in Java with examples
>
The split() method is used to break a given string according to the specified conditions.
String: 123-45678-90
Condition: -
Output : {"123", "45678" , "90"}
### Parameter specified:
****regex**** : condition or regular expression to apply on string for the change required .
****limit****: It tells how much the string to be used in an array.
##### Two usecase of the split() method are as follows:
##### 1. Public String [] split ( String regex, int limit)
Return: Array of string on which regular expreesion is applied is computed and returns the specified result.
Exception Thrown: PatternSyntaxException –when the applied confition is false then this exception occurs.
limit parameter can have 3 choice values:
limit > 0 – Pattern will be applied at most limit -1 times, the output array’s length will not be more than n, and the output array’s last entry will contain all input beyond the last matched pattern.
limit < 0 – Pattern will be applied as many times as possible, and the output array can be of any size.
limit = 0 – Pattern will be applied as many times as possible, the output array can be of any size, and empty strings will be removed.
package MyPackage.
##### Example 1: Calling a split() method on String Object with the limit parameter
public class SplitMethod {
public static void main(String args[])
{
String str = "648-567-7388";
String[] arrOfStr1 = str.split("8",2);
System.out.println("Output when limit is +ve");
System.out.println("Number of substrings: "+arrOfStr1.length);
for(int i=0; i<arrOfStr1.length; i++)
{
System.out.println("str["+i+"] : "+arrOfStr1[i]);
}
String[] arrOfStr2 = str.split("8",-3);
System.out.println("nOutput when limit is -ve");
System.out.println("Number of substrings: "+arrOfStr2.length);
for(int i=0; i<arrOfStr2.length; i++)
{
System.out.println("str["+i+"] : "+arrOfStr2[i]);
}
String[] arrOfStr3 = str.split("8",0);
System.out.println("nOutput when limit is 0");
System.out.println("Number of substrings: "+arrOfStr3.length);
for(int i=0; i<arrOfStr3.length; i++)
{
System.out.println("str["+i+"] : "+arrOfStr3[i]);
}
}
}
Output:
Output when limit is +ve
Number of substrings: 2
str[0] : 64
str[1] : -567-7388
Output when limit is -ve
Number of substrings: 4
str[0] : 64
str[1] : -567-73
str[2] :
str[3] :
Output when limit is 0
Number of substrings: 2
str[0] : 64
str[1] : -567-73
##### Example 2:Working of split(regex, limit) with small limit.
public class Aa {
// Main driver method
public static void main(String args[])
{
// Custom input string
String str = content@for@you";
String[] arrOfStr = str.split("@", 3);
for (String a : arrOfStr)
System.out.println(a);
}
}
OUTPUT
content
for
you
##### Example 3: Working of split(regex,limit) with high limit.
public class Aa {
public static void main(String args[])
{
String str = "hello@everyone@present@here";
String[] arrOfStr = str.split("@", 5);
for (String a : arrOfStr)
System.out.println(a);
}
}
Output
hello
everyone
present
here
##### 2. public String[] split(String regex)
It takes parameter of regular expreasion and when given string mztches with regular espression it breaks out.Default value is Zero.
Parameter: regex (a delimiting regular expression)
Return Value: an array of String
Exception: PatternSyntaxException, if used for invalid syntax.
##### Example : Calling a split() method on String Object – Splitting by a comma
package MyPackage;
public class M1 {
public static void main(String args[])
{
String str = "We're,Ready,to,go!";
String[] arrOfStr = str.split(",");
System.out.println("Number of substrings: "+arrOfStr.length);
for(int i=0; i< arrOfStr.length; i++)
{
System.out.println("str["+i+"] "+arrOfStr[i]);
}
}
}
Output
Number of substrings: 4
str[0] : We're
str[1] : Ready
str[2] : to
str[3] : go
##### Calling a split() method on String Object – Splitting by a dot
package MyPackage;
public class M3 {
public static void main(String args[])
{
String str = "Good.morning.everyone";
String[] arrOfStr = str.split(".");
System.out.println("Number of substrings: "+arrOfStr.length);
for(int i=0; i< arrOfStr.length; i++)
{
System.out.println("str["+i+"] : "+arrOfStr[i]);
}
}
}
Output
Number of substrings: 3
str[0] : Good
str[1] : morning
str[2] : everyone
package MyPackage;
public class M5 {
public static void main(String args[])
{
String str = "We're, Ridiculously Committed! Welcome to course.again";
String[] arrOfStr = str.split("[, .!]+");
System.out.println("Number of substrings: "+arrOfStr.length);
for(int i=0; i< arrOfStr.length; i++)
{
System.out.println("str["+i+"] : "+arrOfStr[i]);
}
}
}
Output
Number of substrings: 7
str[0] : We're
str[1] : Ridiculously
str[2] : Committed
str[3] : Welcome
str[4] : to
str[5] : course
str[6] : again
#### Conclusion:
Spilt method in java provides us with lots of ways to alter aur string wisely and accurately.