java把以逗号分割的字符串进行截取

对于字符串是否包含逗号进行截取,对单个或多个手机号进行截取

public class Test {
    
    
    public static void main(String[] args) {
    
    

        String mobile = "15688888888,13799999999,15266666666";
//        String mobile = "15688888888";
        String[] mobiles = new String[1];
        if (mobile.contains(",")) {
    
    
            mobiles = mobile.split(",");
        } else if (mobile.contains(" ")) {
    
    
            mobiles = mobile.split("  ");
        }else {
    
    
            mobiles[0] = mobile;
        }
        int i = 0;
        for (String telephone : mobiles) {
    
    
            i++;
            System.out.println(telephone);
        }
    }
}

字符串包含逗号打印结果
字符串包含逗号
字符串不包含逗号打印结果
字符串不包含逗号

猜你喜欢

转载自blog.csdn.net/qq_45278500/article/details/124878327
今日推荐