String字符串处理总结

本文介绍如何使用正则表达式去除文本中的特殊字符,并提供了验证手机号码、邮箱地址和密码是否符合标准格式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.去除”& n b s p ;” 中间没有空格,是个占位符

str.replaceAll(“\u00A0”, “”)

2. 正则实用

去除中文

  >str.replaceAll("[^x00-xff]*", "")

去除英文

  > str.replaceAll("[a-zA-Z]","")  

验证手机号码格式()

public static boolean isMobileNO(String mobiles) {
            /*
             * 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
             * 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通)
             * 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
             */
            String telRegex = "[1][345678]\\d{9}";// "[1]"代表第1位为数字1,"[345678]"代表第二位可以为345678中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
            if (TextUtils.isEmpty(mobiles))
                return false;
            else
                return mobiles.matches(telRegex);
        }  

邮箱验证和密码验证

/**
     * 密码验证
     *这里是验证密码长度6到16位,需要数字加字母,也可输入特殊字符
     * @param passWord
     * @return
     */
    public static boolean isPassWordRight(String passWord) {
        String passWordRegex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z@`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]{6,16}$";
        Pattern pattern = Pattern.compile(passWordRegex);
        Matcher matcher = pattern.matcher(passWord);

        if (TextUtils.isEmpty(passWord))
            return false;
        else
            return matcher.matches();
    }

    /**邮箱验证**/
    public static boolean isEmail(String email){
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值