如何判断一个特殊字符是英文的还是中文的?

如何判断一个特殊字符是英文的还是中文的?

问题描述:

我这里要做一个输入的判断,汉字、英文和数字我都能识别出,特殊字符里面分有中文和英文的,不知道怎么区别,各位有没有什么方法解决?

        char[] myChar = s.toCharArray();
        for (int i = 0; i < myChar.length; i++) {
            if ((char) (byte) myChar[i] != myChar[i]) {
                //中文相关字符
            }
        }

试一下这个看看可以么
String str = "我爱你,xr.";
char[] array = str.toCharArray();
int chineseCount = 0;
int englishCount = 0;
for (int i = 0; i < array.length; i++) {
if((char)(byte)array[i]!=array[i]){
chineseCount++;
}else{
englishCount++;
}
}

这个是加入计数后的代码 可以计算中文字符和英文字符个数 其中中文字符包含汉子 英文字符包含字母

String ss = "你";
Pattern pattern=Pattern.compile("[\u4e00-\u9fa5]");  
Matcher matcher=pattern.matcher(ss);  

用正则
matcher为true是中文

public static String distinguish(String src) {
    String result = "";

    Pattern p;
    Matcher m;

    p = Pattern.compile("[\u4e00-\u9fa5]");
    m = p.matcher(src);
    if (m.find()) {
        result = result + "有汉字  ";
    }

    p = Pattern.compile("[a-zA-Z]");
    m = p.matcher(src);
    if (m.find()) {
        result = result + "有字母  ";
    }

    p = Pattern.compile("[0-9]");
    m = p.matcher(src);
    if (m.find()) {
        result = result + "有数字  ";
    }

    p = Pattern.compile("\\p{Punct}");
    m = p.matcher(src);
    if (m.find()) {
        result = result + "有标点符号  ";
    }

    return result;
}

 int[] params = SmsMessage.calculateLength(text, false);
  /* SmsMessage.calculateLength returns an int[4] with:
  *   int[0] being the number of SMS's required,
  *   int[1] the number of code units used,
  *   int[2] is the number of code units remaining until the next message.
  *   int[3] is the encoding type that should be used for the message.

android Mms的源码里是用这个判断的 int[3]是你要参考的值, 只是感觉这么做对你的需求来说有点麻烦了

不知道这样有没有用,可以试试正则表达式

boolean result = yourString.contains("[-+.^:,]");

这个方法检测特殊字符:

Pattern p = Pattern.compile("[&%$#@!()*^]"); //<---- you can add more characters to check here 
Matcher m = p.matcher(myEditText2);
if (m.find()) {
    editText.setError("you can not enter special Character");
    return false;
}

导入包:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

中文的字符ASCII码值在128—255之间(或者是小于零,中文字符一般占用两个字节),英文的字符ASCII码值在0—128之间。

String s="你";
        if(String.valueOf(s.charAt(0)).getBytes().length==2){
            System.out.println("是中文");
        }else{
            System.out.println("不是中文");
        }

public static int count(char tmp) {

    /**空格 */
    int spaceCharacter = 0;

    /**中文字符 */
    int chCharacter = 1;

    /**英文字符 */
    int enCharacter = 2;

    /**数字 */
    int numberCharacter = 3;

    /**其他字符 */
    int otherCharacter = 4;

    if ((tmp >= 'A' && tmp <= 'Z') || (tmp >= 'a' && tmp <= 'z')) {
        return enCharacter;
    } else if ((tmp >= '0') && (tmp <= '9')) {
        return numberCharacter;
    } else if (tmp ==' ') {
        return spaceCharacter;
    } else if (isChinese(tmp)) {
        return chCharacter;
    } else {
        return otherCharacter;
    }
}

/***
 * 判断字符是否为中文
 * @param ch 需要判断的字符
 * @return 中文返回true,非中文返回false
 */
private static boolean isChinese(char ch) {
    //获取此字符的UniCodeBlock
    Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
    //  GENERAL_PUNCTUATION 判断中文的“号
    //  CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
    //  HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
        return true;
    }
    return false;
}

public static void main(String[] args) {
    String test = ",";
    for(int i=0;i<test.length();i++){
      int num = count(test.charAt(i));
      System.out.println(num);
    }
}