您的位置:首页 > 其它

split()方法字符串拆分

2016-05-14 12:26 267 查看
//目的:

// 定义字符串,使用split()方法对字符串进行拆分

//创建类
public class pre1 {
// 主方法
public static void main(String args[]) {
// 定义的字符串str
String str = new String("abc,def,ghi,gkl");
// 使用split()方法对字符串进行拆分,返回字符串数组
String[] newstr = str.split(",");

// 使用for循环遍历字符数组
for (int i = 0; i < newstr.length; i++) {
// 输出信息
System.out.println(newstr[i]);
}

System.out.println("第二次拆分");
// 对字符串进行拆分,并限定拆分字符段数,返回字符数组
String[] newstr2 = str.split("g", 3);
// 循环遍历字符数组
for (int j = 0; j < newstr2.length; j++) {
// 输出信息
System.out.println(newstr2[j]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: