在JAVA中怎么判断你输入的字符是一个中文??? String test="网络部,kkkl;综合"; System.out.print(test.charAt(3)); System.out.println( test.charAt( 3 ) > 256 ? "Yes" : "No" ); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //[\u4E00-\u9FA5]是所有汉字集合的正则表达式String test="汉字";if(test.matches("[\\u4E00-\\u9FA5]+")) System.out.println("contains chinese chars"); String str = "ab测试22d";for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); if(c > 255) { System.out.println("contains chinese chars!!!"); }} 谢谢各们的帮忙,只不过xcopy(xcopy)为什么你的代码我执行不了呀?? .match 在jdk1.4后才出现的你的jdk版本太老了所以没有:) 正则表达式要jdk1.4以上,版本的可以直接判断char的值如果大于等于4e00小于等于9fa5就是汉字,否则不是. 要判断不是拉丁字符很简单,楼上的某些都可以,但是要判断是不是中文就不好办了。或许jdk1.4可以做得到吧,我没用过。kadina(次帅) 的做法,对于日语一样输出“包含中文”的字样! 好象不行啊。不能匹配汉字啊。可以匹配‘=’,代码是:import java.io.*;import java.util.regex.*;/** * Description of the Class * *@author Administrator *@created 2003?¨?6??8?? */public final class RegexTestHarness { private static String REGEX; private static String INPUT; private static BufferedReader br; private static Pattern pattern; private static Matcher matcher; private static boolean found; /** * The main program for the RegexTestHarness class * *@param argv The command line arguments */ public static void main(String[] argv) { initResources(); processTest(); closeResources(); } /** * Description of the Method */ private static void initResources() { try { br = new BufferedReader(new FileReader("regex.txt")); } catch (FileNotFoundException fnfe) { System.out.println("Cannot locate input file! " + fnfe.getMessage()); System.exit(0); } try { REGEX = br.readLine(); INPUT = br.readLine(); } catch (IOException ioe) {} pattern = Pattern.compile(REGEX); matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: " + REGEX); System.out.println("Current INPUT is: " + INPUT); } /** * Description of the Method */ private static void processTest() { while (matcher.find()) { System.out.println("I found the text \"" + matcher.group() + "\" starting at index " + matcher.start() + " and ending at index " + matcher.end() + "."); System.out.println(INPUT.substring(matcher.start(), matcher.end())); found = true; } if (!found) { System.out.println("No match found."); } } /** * Description of the Method */ private static void closeResources() { try { br.close(); } catch (IOException ioe) {} }} 【求助】网址中有空格要用什么代替? true == false 怎么避免空指针异常? Java 定期事务设计 讨论 JAVA C#一个问题 文件输入输出流的问题 现在市面上有没有JAVA描述的数据结构视频教程呀? 急!急!朋友们,请指教 决定开始不学.net 学java了,问一下关于表单验证的小问题 11111 谁的分掉在地上了?快来捡呀!--哪里有详细介绍编写JAVA Bean组件的电子书下载?技术文档也行! 怎样根据查询返回的字段值(数值或字符型值)的宽度动态设置JTabel的Column的宽度 类型转化 时候的 精度丢失问题! 问一个让诸多人翻船的问题
String test="汉字";
if(test.matches("[\\u4E00-\\u9FA5]+")) System.out.println("contains chinese chars");
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(c > 255) {
System.out.println("contains chinese chars!!!");
}
}
你的jdk版本太老了
所以没有
:)
如果大于等于4e00小于等于9fa5就是汉字,否则不是.
不能匹配汉字啊。可以匹配‘=’,代码是:
import java.io.*;
import java.util.regex.*;/**
* Description of the Class
*
*@author Administrator
*@created 2003?¨?6??8??
*/
public final class RegexTestHarness { private static String REGEX;
private static String INPUT;
private static BufferedReader br;
private static Pattern pattern;
private static Matcher matcher;
private static boolean found;
/**
* The main program for the RegexTestHarness class
*
*@param argv The command line arguments
*/
public static void main(String[] argv) {
initResources();
processTest();
closeResources();
}
/**
* Description of the Method
*/
private static void initResources() {
try {
br = new BufferedReader(new FileReader("regex.txt"));
} catch (FileNotFoundException fnfe) {
System.out.println("Cannot locate input file! " + fnfe.getMessage());
System.exit(0);
}
try {
REGEX = br.readLine();
INPUT = br.readLine();
} catch (IOException ioe) {} pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT); System.out.println("Current REGEX is: " + REGEX);
System.out.println("Current INPUT is: " + INPUT);
}
/**
* Description of the Method
*/
private static void processTest() {
while (matcher.find()) {
System.out.println("I found the text \"" + matcher.group() +
"\" starting at index " + matcher.start() +
" and ending at index " + matcher.end() + ".");
System.out.println(INPUT.substring(matcher.start(), matcher.end()));
found = true;
} if (!found) {
System.out.println("No match found.");
}
}
/**
* Description of the Method
*/
private static void closeResources() {
try {
br.close();
} catch (IOException ioe) {}
}
}