Android开发中常用字符处理工具类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

发现开发了几个项目都要用到这些字符处理,资料虽少,但还是简单做了记录,以便以后用得上。

public class VerifyTool {

    /*
     * 邮箱验证
     */
    public static boolean isEmail(String email) {
        return Pattern.compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$").matcher(email).matches();
    }

    /**
     * 验证密码格式, 只支持英文和数字.
     *
     */
    public static boolean verifyPasswordFormat(String pwd) {
        return Pattern.compile("[a-zA-Z0-9]*").matcher(pwd).matches();
    }

    /**
     *
     * @description:验证手机号是否正确(这是根据我们项目需要进行的,实际的不一定都这样)
     */
    public static boolean isMobileNO(String telephone) {
        if (TextUtils.isEmpty(telephone))
            return false;
//        Pattern pattern = Pattern.compile("^0?(13[0-9]|15[012356789]|17[012356789]|18[012356789]|14[57])[0-9]{8}$");
        Pattern pattern = Pattern.compile("^1[3,4,5,7,8][0-9]\\d{8}$");
        Matcher matcher = pattern.matcher(telephone);
        return matcher.matches();
    }

    /**
     * 验证电话号码及是否正确;
     *
     */
    public static boolean isPhoneNO(String mobiles) {
        Pattern p = Pattern.compile("^\\d{10,12}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 验证6位短信验证码是否正确;
     *
     */
    public static boolean isSmsCode(String msgCode) {
        Pattern p = Pattern.compile("^\\d{6}$");
        Matcher m = p.matcher(msgCode);
        return m.matches();
    }

    /**
     * 验证IP和端口的格式是否有误
     *
     * @param ip
     *            访问服务器 IP 地址
     * @param port
     *            访问服务器端口
     * @return 校验ip和端口 return -1 ip 或者 port 为空. -2 ip格式错误. -3 port为数字 0 通过
     * */
    public static final int verificateIPAndPort(String ip, String port) {

        if (null == ip || "".equals(ip) || null == port || "".equals(port)) {
            return -1;
        }

        try {
            Integer.parseInt(port);
        }
        catch (NumberFormatException e) {
            return -3;
        }

        Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
        Matcher matcher = pattern.matcher(ip.trim());
        if (matcher.matches()) {
            return 0;
        }
        else {
            return -2;
        }

    }



    /**
     *
     * 方法概述:
     *
     * @description 方法详细描述:验证输入的金额为整数且只能有两个位小数
     */
    public static boolean monneyFormate(String s) {
        if (TextUtils.isEmpty(s))
            return false;

        Pattern pattern = Pattern.compile("^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?$");
        return pattern.matcher(s).matches();
    }

    /**
     * (判断字符串的长度)
     */
    public static boolean isCheckLength(String str, int start, int end) {
        Pattern pattern = Pattern.compile("^.{" + start + "," + end + "}$");
        return pattern.matcher(str).matches();
    }

    /**
     * @author:tsp
     * @Title: isLength
     * @Description: TODO(判断身份证的长度)
     * @param @param str
     * @param @return
     * @return boolean
     * @throws
     */
    public static boolean isCheckCardLenth(String str, int start, int end) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9].{" + start + "," + end + "}$");
        return pattern.matcher(str).matches();
    }

 
    /**
     *
     * @description:判断是否是身份证格式
     */
    public static boolean checkCardNo(String cardNo) {
        Pattern pattern = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
        Matcher matcher = pattern.matcher(cardNo);
        return matcher.matches();
    }

    /**
     * 判断中文
     */
    public static boolean isChineseChar(String inputString) {
        Pattern pattern = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");
// Pattern pattern = Pattern.compile("^[\\u4E00-\\u9FA5]");
        return pattern.matcher(inputString).matches();
    }

    /**
     * 匹配非负浮点数
     */
    public static boolean isRealNumber(String inputString) {
        Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]{1,5})?$");
        return pattern.matcher(inputString).matches();
    }

    /**
     * 匹配非负浮点数
     */
    public static boolean isNotNegativeFloat(String inputString) {
        Pattern pattern = Pattern.compile("^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$");
        return pattern.matcher(inputString).matches();
    }

    /**
     * 手机号码截取成130****123类型字段,因为用之前方式 的话,诸如17999999990类似号码会截取成:17********0
     */
    public static  String splitePhone(String phone) {
        String[] tel = new String[phone.length()];
        StringBuffer sb = new StringBuffer();
        if (tel.length > 0) {
            for (int i = 0; i < tel.length; i++) {
                if (i > 2 && i < 7) {
                    sb.append("*");
                } else {
                    sb.append(phone.charAt(i));
                }
            }
        }
        return sb.toString();
    }
  }