Java split() Method
The split() method splits the string based on matches of the given regular expression.
Note: Escape characters such as . , $, | and * must be preceded by \.
**Note:** For multiple delimiters, you can use | as the connector.
Syntax
public String[] split(String regex, int limit)
Parameters
-
regex -- The regular expression delimiter.
-
limit -- The number of parts to split into.
Return Value
String array.
Example
Example
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
String
str
=
new
String
(
"
Welcome-to-Runoob
"
)
;
System
.
out
.
println
(
"
- Return value for the delimiter :
"
)
;
for
(
String
retval
:
str
.
split
(
"
"
)
)
{
System
.
out
.
println
(
retval
)
;
}
System
.
out
.
println
(
"
"
)
;
System
.
out
.
println
(
"
- Return value when the delimiter sets the number of parts :
"
)
;
for
(
String
retval
:
str
.
split
(
"
"
,
2
)
)
{
System
.
out
.
println
(
retval
)
;
}
System
.
out
.
println
(
"
"
)
;
String
str2
=
new
String
(
"
www.runoob.com
"
)
;
System
.
out
.
println
(
"
Return value for escape characters :
"
)
;
for
(
String
retval
:
str2
.
split
(
"
\
.
"
,
3
)
)
{
System
.
out
.
println
(
retval
)
;
}
System
.
out
.
println
(
"
"
)
;
String
str3
=
new
String
(
"
acount=? and uu =? or n=?
"
)
;
System
.
out
.
println
(
"
Return value for multiple delimiters :
"
)
;
for
(
String
retval
:
str3
.
split
(
"
and|or
"
)
)
{
System
.
out
.
println
(
retval
)
;
}
}
}
The execution result of the above program is:
Delimiter return value:
Welcome
to
Runoob
- Separator sets the number of splits return:
Welcome
to-Runoob
Escape character return value :
www
runoob
com
Multiple delimiter return values :
acount=?
uu =?
n=?
Java String class