Java的split()是怎么拆分字符串的?
1个回答
2018-01-31 · 知道合伙人互联网行家
关注
展开全部
java拆分字符串使用string类的spilt方法,针对某个分隔符来分割一个字符串,示例如下:
public class StringSplit {
public static void main(String[] args) {
String sourceStr = "1,2,3,4,5";//一个字符串
String[] sourceStrArray = sourceStr.split(",");//分割出来的字符数组
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}
// 最多分割出3个字符串
int maxSplit = 3;
sourceStrArray = sourceStr.split(",", maxSplit);
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}
}
}
输出结果为:
2
4
1
3,4,5
public class StringSplit {
public static void main(String[] args) {
String sourceStr = "1,2,3,4,5";//一个字符串
String[] sourceStrArray = sourceStr.split(",");//分割出来的字符数组
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}
// 最多分割出3个字符串
int maxSplit = 3;
sourceStrArray = sourceStr.split(",", maxSplit);
for (int i = 0; i < sourceStrArray.length; i++) {
System.out.println(sourceStrArray[i]);
}
}
}
输出结果为:
2
4
1
3,4,5
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询