09-09
18
判斷輸入的字串中,中英文判別
作者:Java伴侣 日期:2009-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; }
}
}
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; }
}
}
评论: 0 | 引用: 0 | 查看次数: 283
发表评论