Java intercepts comma-separated strings

Intercept whether the string contains commas and intercept single or multiple mobile phone numbers.

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);
        }
    }
}

Print the result if the string contains commas. Print
String contains comma
the result if the string does not contain commas.
String does not contain commas

Guess you like

Origin blog.csdn.net/qq_45278500/article/details/124878327