其中separator表示分隔符,limit表示返回数组的元素个数
注意:
示例1:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ");
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain
示例2:
public class SplitDemo {程序结果:
The
rain in Spain falls mainly in the plain
示例3:
public class SplitDemo3 {
public static String[] ss = new String[20];
public SplitDemo3() {
String s = "The rain in Spain falls mainly in the plain.";
ss = s.split(" ", 20); // 在每个空格字符处进行分解
}
public static void main(String[] args) {
SplitDemo3 demo = new SplitDemo3();
for (int i = 0; i < ss.length; i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.