split 方法
將一個字符串分割為子字符串,然后將結果作為字符串數組返回。
stringObj.split([separator,[limit]])
limit
可選項。該值用來限制返回數組中的元素個數。
示例:
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(" ", 2);
}
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.