加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:50:26 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class VerifyTool { /* * 邮箱验证 */ public static boolean isEmail(String email) { return Pattern.compile("^w+((-w+)|(.w+))*

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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((?!ddd)d+|1dd|2[0-4]d|25[0-5]).((?!ddd)d+|1dd|2[0-4]d|25[0-5]).((?!ddd)d+|1dd|2[0-4]d|25[0-5]).((?!ddd)d+|1dd|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 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-u9FA5uF900-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();
    }
  }


以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读