判断 028 - 88823423 有没有输入"-" 和区号是否为三位 号码是否为八位,求解
用 split 来实现

解决方案 »

  1.   

    其实还是比较简单的,试试这段代码
    public static void test(String s){
    String[] strings=s.split("-");
    if(strings.length!=0&&strings!=null){
    System.out.println("包含字符“-”");
    }

    if(strings[0].trim().length()==3){
    System.out.println("区号为3位");
    }

    if(strings[1].trim().length()==8){
    System.out.println("区号为8位");
    }
    }
      

  2.   

    s.split("-");,返回的数组,下标为0的strings[0]表示分割后的第一部分,也就是前3位数,trim()就是去除首尾空格。。不过一楼代码有点问题,还是用s.contains("-")来判断是否包含字符-吧
      

  3.   


    public class Test {
    public static void main( String[] args ) {
    String code = "020-8073213";
    String phone;
    if( code.contains("-") ) {
    String[] strs = code.split("-");

    if( strs[0].length()==3 ) System.out.println( "区号是3位" );
    else System.out.println( "区号不是3位" );

    phone = strs[1];
    }else{
    phone = code;
    }

    if( phone.length()==8 ) System.out.println( "号码是8位" );
    else System.out.println( "号码不是8位" );
    }
    }
      

  4.   

    不包含"-"字符,就无所谓3位,8位了。注释的行为一些测试数据,在一楼的基础上修改:public class Test {
    public static void main(String[] args) {
    String s = "028  88823423";
    //String s = "028 - 88823423";
    //String s = "028";
    //String s = "";

    String[] strings=s.split("-");

    if(strings.length == 2){
    System.out.println("包含字符-");

    if(strings[0].trim().length()==3) {
    System.out.println("区号为3位");
    } else {
    System.out.println("区号不为3位");
    }

    if(strings[1].trim().length()==8) {
    System.out.println("区号为8位");
    } else {
    System.out.println("区号不为8位");
    }
    }

    System.out.println("不包含字符-");

    }
    }