Java Example – String splitting
The following example uses the split(string) method to split a string into an array by a specified delimiter:
JavaStringSplitEmp.java file
public
class
JavaStringSplitEmp
{
public
static
void
main
(
String
args
[
]
)
{
String
str
=
"
www-runoob-com
"
;
String
[
]
temp
;
String
delimeter
=
"
"
;
//
Specify the split character
temp
=
str
.
split
(
delimeter
)
;
//
Split the string
//
Ordinary for loop
for
(
int
i
=
0
;
i
<
temp
.
length
;
i
++
)
{
System
.
out
.
println
(
temp
[
i
]
)
;
System
.
out
.
println
(
"
"
)
;
}
System
.
out
.
println
(
"
------java for each loop output method-----
"
)
;
String
str1
=
"
www.runoob.com
"
;
String
[
]
temp1
;
String
delimeter1
=
"
\
.
"
;
//
Specify the split character; the . needs to be escaped
temp1
=
str1
.
split
(
delimeter1
)
;
//
Split the string
for
(
String
x
:
temp1
)
{
System
.
out
.
println
(
x
)
;
System
.
out
.
println
(
"
"
)
;
}
}
}
The output of the above code example is:
www
runoob
com
------java for each循环输出的方法-----
www
runoob
com
Java Examples