split()方法字符串拆分

来源:互联网 发布:淘宝瑕疵大牌化妆品 编辑:程序博客网 时间:2024/06/04 19:57

//目的:
// 定义字符串,使用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]);        }    }}
0 0
原创粉丝点击