split()--what to split the string with.

package homework;

//1、统计当前字符串中所罗列的人数。
public class practice_01 {
    public static void main(String[] args) {
        String str = "张飞,刘备,关羽,曹操,关平,貂蝉";
        //split(",")将字符串以 , 分割开来。
        String[] names = str.split(",");
        int count = 0;  //定义一个统计变量。
        for (String name : names) {     //遍历数组name
            count++;    //开始计数
//            System.out.println(name);  //此行为遍历输出数组name的元素
        }
        //输出统计结果
        System.out.println("一共有" + count + "人");    
    }
}

Guess you like

Origin blog.csdn.net/yl23921/article/details/126736374