Check if a string has Chinese using regex to check unicode range - Java Internationalization

Java examples for Internationalization:Chinese

Description

Check if a string has Chinese using regex to check unicode range

Demo Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(hasChinese(str));
    }/*from w w  w. ja  va  2s . c om*/

    public static boolean hasChinese(String str) {
        int count = 0;
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            int size = m.groupCount();
            for (int i = 0; i <= size; i++) {
                count = count + 1;
            }
        }
        return count > 0;
    }
}

Related Tutorials