切割字符串:
String str = “1,2,3,4,5”;
String[] strArray = str.split(",");
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
// 最多分割出3个字符串
int maxSplit = 3;
strArray = str.split(",", maxSplit);
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
注意:
“.”和“|”都是转义字符,必须得加"\";
如果用“.”作为分隔的话,必须是如下写法:
String.split("\."),这样才能正确的分隔开,不能用String.split(".");
如果用“|”作为分隔的话,必须是如下写法:
String.split("\|"),这样才能正确的分隔开,不能用String.split("|");
删除最后一个字符:
字符串:string s = “1,2,3,4,5,”
s = s.Substring(0,s.Length - 1)