public String[] split(String regex,int limit)根据匹配给定的正则表达式来拆分此字符串。
此方法返回的数组包含此字符串的每个子字符串,这些子字符串由另一个匹配给定的表达式的子字符串终止或由字符串结束来终止。数组中的子字符串按它们在此字符串中的顺序排列。如果表达式不匹配输入的任何部分,则结果数组只具有一个元素,即此字符串。
limit 参数控制模式应用的次数,因此影响结果数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后项将包含超出最后匹配的定界符的所有输入。如果 n 为非正,则模式将被应用尽可能多的次数,而且数组可以是任意长度。如果 n 为零,则模式将被应用尽可能多的次数,数组可有任何长度,并且结尾空字符串将被丢弃。
示例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);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.
示例2:
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);
}
}
程序结果:
The
rain in Spain falls mainly in the plain.
示例3:
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(" ", 20);
}
public static void main(String[] args) {
SplitDemo demo = new SplitDemo();
for (int i = 0; i < ss.length; i++)
System.out.println(ss);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.
示例4:
public class SplitDemo {
public static void main(String[] args) {
String value = "192.168.128.33";
String[] names = value.split(".");
for (int i = 0; i < names.length; i++) {
System.out.println(names);
}
}
}
运行结果:
对,没看错!没有任何输出!
http://blog.csdn.net/lizhengnanhua/article/details/7994159