欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中判断字符串是中文或者英文的工具类分享

程序员文章站 2024-03-01 19:16:04
直接上代码: 复制代码 代码如下: import java.util.regex.matcher; import java.util.regex.pattern;...

直接上代码:

复制代码 代码如下:

import java.util.regex.matcher;
import java.util.regex.pattern;

/**
 *
 * <p>
 * classname showchineseinunicodeblock
 * </p>
 * <p>
 * description 提供判断字符串是中文或者是英文的一种思路
 * </p>
 *
 * @author wangxu wangx89@126.com
 *         <p>
 *         date 2014-9-16 下午06:45:35
 *         </p>
 * @version v1.0
 *
 */
public class showchineseinunicodeblock {
 public static void main(string[] args) {
  string str = "我爱你,!?():;“”、。";
  char[] chararray = str.tochararray();
  for (int i = 0; i < chararray.length; i++) {
   ischinese(chararray[i]);
  }
  string chinese = "中国god damn";
  system.out.println(iscontainchinese(chinese));
  string english = "dfafdabac";
  system.out.println(isenglish(english));
 }

 /**
  *
  * <p>
  * title: ischinese
  * </p>
  * <p>
  * description: 该函数就用来打印一些字符看看属于什么
  * </p>
  *
  * @param c
  *
  */
 public static void ischinese(char c) {
  character.unicodeblock ub = character.unicodeblock.of(c);
  if (ub == character.unicodeblock.cjk_unified_ideographs) {
   system.out.println(c + "--cjk_unified_ideographs");
  } else if (ub == character.unicodeblock.cjk_compatibility_ideographs) {
   system.out.println(c + "--cjk_compatibility_ideographs");
  } else if (ub == character.unicodeblock.cjk_unified_ideographs_extension_a) {
   // cjk unified ideographs extension wikipediaunicode扩展汉字
   // cjk unified ideographs extension a 中日韩统一表意文字扩展区a ; 表意文字扩充a
   // cjk unified ideographs extension b 中日韩统一表意文字扩展区b
   system.out.println(c + "--cjk_unified_ideographs_extension_a");
  } else if (ub == character.unicodeblock.general_punctuation) {// 通用标点
   system.out.println(c + "--general_punctuation");

  } else if (ub == character.unicodeblock.cjk_symbols_and_punctuation) {
   system.out.println(c + "--cjk_symbols_and_punctuation");

  } else if (ub == character.unicodeblock.halfwidth_and_fullwidth_forms) {
   system.out.println(c + "--halfwidth_and_fullwidth_forms");

  }
 }

 public static boolean isenglish(string charastring) {
  return charastring.matches("^[a-za-z]*");
 }

 public static boolean iscontainchinese(string str) {// 检测是否包含中文
  string regex = "[\\u4e00-\\u9fa5]+";
  pattern p = pattern.compile(regex);
  matcher m = p.matcher(str);
  if (m.find()) {
   return true;
  } else {
   return false;
  }
 }
}