android 将手机号中间隐藏为星号(*)和手机号码判断

  //截取手机号码 方法一
                                        String phonenum = "15718807588";
                                        if(!TextUtils.isEmpty(phonenum) && phonenum.length() > 6 ){
                                            StringBuilder sb  =new StringBuilder();
                                            for (int i = 0; i < phonenum.length(); i++) {
                                                char c = phonenum.charAt(i);
                                                if (i >= 3 && i <= 6) {
                                                    sb.append('*');
                                                } else {
                                                    sb.append(c);
                                                }
                                            }
 
                                            ToastUtil.showImgMessage(context,sb.toString()+"1");
                                        }
  //方法二
                                        phonenum = phonenum.substring(0, 3) + "****" + phonenum.substring(7, 11);
                                        ToastUtil.showImgMessage(context,phonenum+"2");
//方法三:用****替换手机号码中间4位
String mobile = "15718807588";
String maskNumber = mobile.substring(0,3)+"****"+mobile.substring(7,mobile.length());
/**
     * 检查是否是电话号码
     * 
     * @return
     */
    public static boolean isMobileNum(String mobiles) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

猜你喜欢

转载自blog.csdn.net/GodnessIsMyMine/article/details/82500620