Split() String method in Java with examples
The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a String array. Example: Following are the two variants of the split() method in Java: Parameters: Returns: An array of strings is computed by splitting the given string. Throws: PatternSyntaxException – if the provided regular expression’s syntax is invalid. The limit parameter can have 3 values: Here’s how it works: Following are the Java example codes to demonstrate the working of split() Example 2: Example 3: Example 4: Example 5: Example 6: This variant of the split method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here, by default limit is 0. Parameters: regex < Returns: An array of strings is computed by splitting the given string. Throws: PatternSyntaxException – if the provided regular expression’s syntax is invalid. Here are some working example codes: Example 2: It can be seen in the above example that the pattern/regular expression “for” is applied twice (because “for” is present two times in the string to be split) Example 4: In the above example that trailing empty strings are not included in the resulting array arrOfStr. In the above example, the trailing spaces (hence not empty string) become a string in the resulting array arrOfStr. In the above example, words are separated whenever either of the characters specified in the set is encountered. This article is contributed by Vaibhav Bajpai. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect or you want to share more information about the topic discussed above.
Input String: 016-78967
Regular Expression: -
Output : {"016", "78967"}
1. Public String [ ] split ( String regex, int limit )
Let the string that is to be split is – geekss@for@geekssRegex Limit Result @ 2 {“geekss”, ”for@geekss”} @ 5 {“geekss”, ”for”, ”geekss”} @ -2 {“geekss”, ”for”, ”geekss”} s 5 {“geek”, ”“, “@for@geek”, “”, “”} s -2 {“geek”, ” “, ” “, “@for@geek”, “”, “”} s 0 {“geek”, ””, ”@for@geek”}
Example 1: Java
// Java program to demonstrate working of split(regex,
// limit) with small limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"@"
,
2
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geekss
for@geekss
Java
// Java program to demonstrate working of split(regex,
// limit) with high limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"@"
,
5
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geekss
for
geekss
Java
// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"@"
, -
2
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geekss
for
geekss
Java
// Java program to demonstrate working of split(regex,
// limit) with high limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"s"
,
5
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geek
@for@geek
Java
// Java program to demonstrate working of split(regex,
// limit) with negative limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"s"
, -
2
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geek
@for@geek
Java
// Java program to demonstrate working of split(regex,
// limit) with 0 limit.
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"geekss@for@geekss"
;
String[] arrOfStr = str.split(
"s"
,
0
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
geek
@for@geek
2. public String[] split(String regex)
Example 1:Java
// Java program to demonstrate working of split()
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"GeeksforGeeks:A Computer Science Portal"
;
String[] arrOfStr = str.split(
":"
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
GeeksforGeeks
A Computer Science Portal
Java
// Java program to demonstrate working of split()
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"GeeksforGeeksforStudents"
;
String[] arrOfStr = str.split(
"for"
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
Geeks
Geeks
Students
Example 3: Java
// Java program to demonstrate working of split()
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"Geeks for Geeks"
;
String[] arrOfStr = str.split(
" "
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
Geeks
for
Geeks
Java
// Java program to demonstrate working of split()
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"Geekssss"
;
String[] arrOfStr = str.split(
"s"
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
Geek
Example 5: Java
// Java program to demonstrate working of split()
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"GeeksforforGeeksfor "
;
String[] arrOfStr = str.split(
"for"
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
Geeks
Geeks
Example 6: Java
// Java program to demonstrate working of split()
// using regular expressions
public
class
GFG {
public
static
void
main(String args[])
{
String str =
"word1, word2 word3@word4?word5.word6"
;
String[] arrOfStr = str.split(
"[, ?.@]+"
);
for
(String a : arrOfStr)
System.out.println(a);
}
}
word1
word2
word3
word4
word5
word6