Splitdemo類對給定的字元串s1使用split()方法,以“ ”和“,”将每個單詞分隔。分割産生的字元串數組由s2接受,最後将s2周遊輸出。
public class Splitdemo
{
public static void main (String [] args)
{
String s1="Solutions to selected exercises can be found in the electronic "
+ "document The ThinKing in Java Annotated Soulation Gide,available "
+ "for a small fee from BruceEckel";
String []s2=s1.split( " |,");
for (String i:s2)
System.out.println(i);
}
}
Solutions
to
selected
exercises
can
be
found
in
the
electronic
document
The
ThinKing
in
Java
Annotated
Soulation
Gide
available
for
a
small
fee
from
BruceEckel