java判斷字元中文
1. java怎麼檢驗字元串是否全為純中文
Java判斷一個字元串是否有中文一般情況是利用Unicode編碼(CJK統一漢字的編碼區間:0x4e00–0x9fbb)的正則來做判斷,但是其實這個區間來判斷中文不是非常...
2. java判斷字元串中是否含有中文
publicclasstest{
publicstaticvoidmain(String[]args){
System.out.println(isContainsChinese("122地點"));
}
//方法返回true為包含中文;false不包含
(Stringstr)
{
Patternpat=Pattern.compile("[u4e00-u9fa5]");
Matchermatcher=pat.matcher(str);
booleanflg=false;
if(matcher.find()){
flg=true;
}
returnflg;
}
}
3. java判斷字元串中是否有中文
把要判斷的字元串放入List裡面,然後遍歷list集合,如果還有指定的字元就輸出,如下代碼:
packagecom.qiu.lin.he;
importjava.util.ArrayList;
importjava.util.List;
publicclassCeshi{
publicstaticvoidmain(String[]args){
List<String>list=newArrayList<String>();//新建一個集合
list.add("puton");
list.add("inonputin");
list.add("oneputonininputoutoutput");
for(Strings:list){
if(s.indexOf("puton")!=-1){//如果含有連續的字元puton則輸出yes
System.out.println(s+"----yes");
}else{
System.out.println(s+"----no");
}
}
}
}
運行結果如下:
4. Java 判斷字元串中是否含有漢字
Java判斷字元串中是否包含漢字
Java代碼
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IfHanZi {
public static void main(String[] args) {
//方法一:
String s1 = "我是中國人";
String s2 = "imchinese";
String s3 = "im中國人";
System.out.println(s1 + ":" + new String(s1).length());
System.out.println(s2 + ":" + new String(s2).length());
System.out.println(s3 + ":" + new String(s3).length());
System.out.println((s1.getBytes().length == s1.length()) ? "s1無漢字":"s1有漢字");
System.out.println((s2.getBytes().length == s2.length()) ? "s2無漢字":"s2有漢字");
System.out.println((s3.getBytes().length == s3.length()) ? "s3無漢字":"s3有漢字");
//方法二:
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
String str = "中文fd我是中國人as ";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "個 ");
}
}
5. java判斷字元是不是中文
下滿給出示例代碼,希望對你有幫助Java中判斷字元串的編碼有兩種思路:一種是根據byte的長度判斷,英文的字母數字好標點符號都是一個byte,且值在0-255之間另一種是根據中文的Unicode取值范圍判斷,這個就是把所以的范圍都包含,才能判斷正確,參考unicode中文范圍:http://www.yuanxingyuan.com/?id=14示例代碼:import java.util.regex.Matcher;import java.util.regex.Pattern;public class StringTest { //英文佔1byte,非英文(可認為是中文)佔2byte,根據這個特性來判斷字元 public static boolean checkChar(char ch) { if ((ch + "").getBytes().length == 1) { return true;//英文 } else { return false;//中文 } } public static String checkString(String str) { String res = ""; if (str != null) { for (int i = 0; i < str.length(); i++) { //只要字元串中有中文則為中文 if (!checkChar(str.charAt(i))) { res = "中文"; break; } else { res = "英文"; } } } return res; } //判斷是不是中文 public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } //判斷是不是英文字母 public static boolean isEnglish(String charaString) { return charaString.matches("^[a-zA-Z]*"); } //根據中文unicode范圍判斷u4e00 ~ u9fa5不全 public static String isChinese(String str) { String regEx1 = "[\\u4e00-\\u9fa5]+"; String regEx2 = "[\\uFF00-\\uFFEF]+"; String regEx3 = "[\\u2E80-\\u2EFF]+"; String regEx4 = "[\\u3000-\\u303F]+"; String regEx5 = "[\\u31C0-\\u31EF]+"; Pattern p1 = Pattern.compile(regEx1); Pattern p2 = Pattern.compile(regEx2); Pattern p3 = Pattern.compile(regEx3); Pattern p4 = Pattern.compile(regEx4); Pattern p5 = Pattern.compile(regEx5); Matcher m1 = p1.matcher(str); Matcher m2 = p2.matcher(str); Matcher m3 = p3.matcher(str); Matcher m4 = p4.matcher(str); Matcher m5 = p5.matcher(str); if (m1.find() || m2.find() || m3.find() || m4.find() || m5.find()) return "中文"; else return "英文"; } public static void main(String[] args) { System.out.println("使用長度判斷:"); System.out.println(checkString("Hello++")); System.out.println(checkString("Hello++。、,?")); System.out.println(checkString("Hello++編程")); System.out.println(checkString("編程")); System.out.println("\r\n使用正則表達式判斷:"); System.out.println(isChinese("Hello++")); System.out.println(isChinese("Hello++。、,?")); System.out.println(isChinese("Hello++編程")); System.out.println(isChinese("編程")); System.out.println("\r\n使用Character.UnicodeBlock"); System.out.println(isChinese('h')?"中文":"英文"); System.out.println(isChinese(',')?"中文":"英文"); System.out.println(isChinese('。')?"中文":"英文"); System.out.println(isChinese('編')?"中文":"英文"); }}運行結果:使用長度判斷:英文中文中文中文使用正則表達式判斷:英文中文中文中文使用Character.UnicodeBlock英文英文中文中文
6. java怎麼判斷輸入的是不是中文
你好,最簡單而且最保險的一種方法是:
這里舉個最復雜的例子:就是輸入了中文和英文的
String str = "....." ; //str是你輸入的東西
如果str.length() < str.getBytes().length 那麼輸入的肯定有中文
看測試代碼:
public class Chinese {
public static void main(String[] args) {
String str1 = "aaa" ;
String str2 = "中國" ;
String str3 = "中a" ;
isChinese(str1) ;
isChinese(str2) ;
isChinese(str3) ;
}
public static void isChinese(String str){
if(str.length() < str.getBytes().length){
System.out.println("有中文");
}else{
System.out.println("英文");
}
}
}
7. java 判斷字元是否為漢字
java判斷是否為漢字 Java代碼如下:
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;i<chars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0]& 0xff;
ints[1]=bytes[1]& 0xff;
if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
public boolean vd(String str){
char[] chars=str.toCharArray();
boolean isGB2312=false;
for(int i=0;i<chars.length;i++){
byte[] bytes=(""+chars[i]).getBytes();
if(bytes.length==2){
int[] ints=new int[2];
ints[0]=bytes[0]& 0xff;
ints[1]=bytes[1]& 0xff;
if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
isGB2312=true;
break;
}
}
}
return isGB2312;
}
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
這兩個包,接下來是代碼
Java代碼
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile(」[0-9]*」);
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ) {
return false;
}
return true;
}
java.lang.Character.isDigit(ch[0])
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile(」[0-9]*」);
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ) {
return false;
}
return true;
}
java.lang.Character.isDigit(ch[0])
-----------------另一種-----------------
Java代碼
public static void main(String[] args) {
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
//System.out.println(regEx);
String str = "中文fdas ";
//System.out.println(str);
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "個 ");
}
public static void main(String[] args) {
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
//System.out.println(regEx);
String str = "中文fdas ";
//System.out.println(str);
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "個 ");
} -------------------------------------------------------------------
8. java 判斷字元串是否含有中文
Java代碼
package com.zakisoft.ch;
public class IsChineseOrNot {
// GENERAL_PUNCTUATION 判斷中文的「號
// CJK_SYMBOLS_AND_PUNCTUATION 判斷中文的。號
// HALFWIDTH_AND_FULLWIDTH_FORMS 判斷中文的,號
private static final boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
public static final boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("測試"));
System.out.println(isChinese("「測試」,。?!%……&*()——{}【】」"));
}
public static final boolean isChineseCharacter(String chineseStr) {
char[] charArray = chineseStr.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
return true;
}
}
return false;
}
/**
* @deprecated; 棄用。和方法isChineseCharacter比效率太低。
* */
public static final boolean isChineseCharacter_f2() {
String str = "!?";
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).matches("[\\一-\\?]+")) {
return true;
}
}
return false;
}
}
System.out.println(isChinese("き"));
System.out.println(isChinese("test,.?!%^&*(){}[]"));
System.out.println(isChinese("測試"));
System.out.println(isChinese("「測試」,。?!%……&*()——{}【】」"));
運行結果為:
false
false
true
true