Android之字符串判断中文、英文 和 数字

Android之字符串判断中文、英文 和 数字,第1张

概述检测一个字符时:/***验证单个汉字,多个就会输出不是汉字**@paramtext*/privatevoidvalidate(Stringtext){if(text.length()==1){Patternp=Pattern.compile("[\u4e00-\u9fa5]");Matcherm=p

检测一个字符时:

    /**     * 验证单个汉字,多个就会输出不是汉字     *     * @param text     */    private voID valIDate(String text) {        if (text.length() == 1) {            Pattern p = Pattern.compile("[\u4e00-\u9fa5]");            Matcher m = p.matcher(text);            if (m.matches()) {                Toast.makeText(this, "输入是汉字!", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(this, "输入的值不是汉字", Toast.LENGTH_SHORT).show();            }        } else {            Toast.makeText(this, "只能输入一个值", Toast.LENGTH_SHORT).show();        }    }

检测多个字符的方法:

/**     * 验证 是否 含有汉字     *     * @param text     */    private voID valIDate2(String text) {        Log.i("info input text:", text);        if (isChinese(text)) {            Toast.makeText(this, "含有中文", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(this, "输入值未含有中文", Toast.LENGTH_SHORT).show();        }    }    /**     * 判断一个字符串是否含有中文     */    public boolean isChinese(String str) {        if (str == null)            return false;        for (char c : str.tochararray()) {            if (isChineseChar(c))                return true;// 有一个中文字符就返回        }        return false;    }    // 判断一个字符是否是中文    public boolean isChineseChar(char c) {        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断    }

检测多个字符的另一种方法:     

/**     * 检查是否包含汉字     *     * @param text     */    private voID valIDate3(String text) {        if (isHasChinese(text)) {            Toast.makeText(this, "输入值含有汉字!", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(this, "没有汉字", Toast.LENGTH_SHORT).show();        }    }    /**     * 是否包括中文     *     * @param content     * @return     */    public static boolean isHasChinese(String content) {        String regEx = "[\u4e00-\u9fa5]";        Pattern pattern = Pattern.compile(regEx);        Matcher matcher = pattern.matcher(content);        while (matcher.find()) {            return true;        }        return false;    }

检测多个字符  会否含有连续的几个中文:

    /**     * 是否含有连续的几个中文     * @param content 内容     * @param count 数量     * @return     */    public static boolean isHasContinuousChinese(String content,int count) {        String regEx = "[\u4e00-\u9fa5]";        for(int i = 0; i < count - 1; i++) {            regEx += regEx;        }        Pattern pattern = Pattern.compile(regEx);        Matcher matcher = pattern.matcher(content);        while(matcher.find()) {            return true;        }        return false;    }

检测字符串里 中文的数量:

    /**     * 得到字符串里中文的数量     * @param content     * @return     */    public static int getChineseCount(String content) {        int count = 0;        String regEx = "[\u4e00-\u9fa5]";        Pattern pattern = Pattern.compile(regEx);        Matcher matcher = pattern.matcher(content);        while(matcher.find()) {            count ++;        }        return count;    }

检测输入的字符是否英文

    /**     * 验证值,是否是英文     *     * @param text     */    private voID valIDateEn(String text) {        Pattern p = Pattern.compile("[a-zA-Z]+");        Matcher m = p.matcher(text);        if (m.matches()) {            Toast.makeText(this, "输入的值是英文", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(this, "输入的值中含有非英文字母", Toast.LENGTH_SHORT).show();        }    }

检测输入的字符串中是否包含英文字母:

    /**     * 判断是否包含英文字母     *     * @param str     * @return     */    public boolean containEnglishLetters(String str) {        String regex = ".*[a-zA-Z]+.*";        Matcher m = Pattern.compile(regex).matcher(str);        if (m.matches()){            Toast.makeText(this, "输入的值包含英文字母", Toast.LENGTH_SHORT).show();        }else {            Toast.makeText(this, "输入的值中不包含英文字母", Toast.LENGTH_SHORT).show();        }        return m.matches();    }

检测输入的值是否为数字:

    /**     * 判断输入的值是否数字     * @param text     */    private voID valIDateNum(String text) {        Pattern p = Pattern.compile("[0-9]*");        Matcher m = p.matcher(text);        if (m.matches()) {            Toast.makeText(this, "输入是数字!", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(this, "输入的值中含有非数字", Toast.LENGTH_SHORT).show();        }    }

 

总结

以上是内存溢出为你收集整理的Android之字符串判断中文、英文 和 数字全部内容,希望文章能够帮你解决Android之字符串判断中文、英文 和 数字所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存