Java String 类型 的拆分 判断 028 - 88823423 有没有输入"-" 和区号是否为三位 号码是否为八位,求解用 split 来实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实还是比较简单的,试试这段代码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位"); }} s.split("-");,返回的数组,下标为0的strings[0]表示分割后的第一部分,也就是前3位数,trim()就是去除首尾空格。。不过一楼代码有点问题,还是用s.contains("-")来判断是否包含字符-吧 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位" ); }} 不包含"-"字符,就无所谓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("不包含字符-"); }} ObjectInputStream 中static变量仍可能修改和存储 怎么对输入的日期格式验证? 高手请进,如何让JTree的根节点和第一级节点不可编辑,而其他节点可编辑呢? 请需要c#全站程序和mis oa程序的网友进来看看 如何创建和解压一个带密码的zip包? 谁有effective java 的中文版? 问个小问题:要是PrintWriter关闭了,OutputStream会给关闭吗? 怎么老是这个错误,帮我瞅一瞅,谢了先!!! 急!用javabean实现下载功能的后下载的文件变乱码 想问一下中文显示的问题,急送分! 有个问题,求大神帮忙解答 String 字符串
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位");
}
}
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位" );
}
}
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("不包含字符-");
}
}