09-09
18

判斷輸入的字串中,中英文判別

要判斷中文得先知道中文 unicode 的 range,
希望下面例子對你有幫助

public class Test{
public static void main(String[] args) {
String test = "Is This 123 中文 or 不是";
System.out.print("char\t");
System.out.print("unicode\t");
System.out.println("hex\t");
System.out.println("---------------------------------");
for (int i = 0; i < test.length(); i++) {
char c = test.charAt(i);
processChar(c);
}
}

private static void processChar(char c) {
int i = (int)c;

System.out.print(c + "\t");
System.out.print((int)c + "\t");
System.out.print(Integer.toHexString(i) + "\t");


if ( (i >= 48) && (i <= 57) ) { System.out.println("this is number"); return; }
if ( (i >= 65) && (i <= 90) ) { System.out.println("this is uppercase letter"); return; }
if ( (i >= 97) && (i <= 122) ) { System.out.println("this is lowercase letter"); return; }
if (i == 32) { System.out.println("this is space"); return; }
if (i > 256) { System.out.println("this may be chinese, japanese...."); return; }
}
}


[本日志由 blurxx 于 2009-09-18 04:02 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 英文 中文 判断
相关日志:
评论: 0 | 引用: 0 | 查看次数: 242
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭