count Chinese Words by regex - Java Internationalization

Java examples for Internationalization:Charset

Description

count Chinese Words by regex

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        String text = "java2s.com";
        System.out.println(countChineseWords(text));
    }/*from ww  w .j a va 2  s  .  c om*/

    private static final Pattern ChineseCharPatt = Pattern
            .compile("[\u4e00-\u9fa5]");

    public static int countChineseWords(String text) {
        Matcher m = ChineseCharPatt.matcher(text);
        int count = 0;
        while (m.find())
            count++;
        return count;
    }
}

Related Tutorials