Java computation contains Chinese character string length __java

Source: Internet
Author: User

Package Com.youku.paycenter.admin.subGate.service.impl;

Import java.io.UnsupportedEncodingException;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

public class TestString
{

public static final String chinese_reg_exp = "[\u4e00-\u9fa5]"; public static void Main (string[] args) throws Unsupportedencodingexception {String s = "The Yellow crane Tower, the fireworks under the march of Yangzhou; lonely sail Far Shadow Azure sky, only see the Yangtze River days Inter flow. The Yellow Crane Tower, the words of the West, fireworks in the march of Yangzhou, Lone sail far shadow Azure, only see the Yangtze River Horizon flow. The Yellow Crane Tower, the words of the West, fireworks in the march of Yangzhou, Lone sail far shadow Azure, only see the Yangtze River Horizon flow. The Yellow Crane Tower, the words of the West, fireworks in the march of Yangzhou, Lone sail far shadow Azure, only see the Yangtze River Horizon flow. The Yellow Crane Tower, the words of the West, fireworks in the march of Yangzhou, Lone sail far shadow Azure, only see the Yangtze River Horizon flow. The Yellow Crane Tower, the words of the West, fireworks in the march of Yangzhou, Lone sail far shadow Azure, only see the Yangtze River Horizon flow.

    The Yellow Crane Tower, abc "of the western words;

        if (Ischinesechar (s)) {char[] C_array = S.tochararray ();

        String c_string = null;

        Byte[] C_byte_array;

        int chinesecount = 0;

        int normalcount = 0;

            for (char c:c_array) {c_string = character.tostring (c);

            C_byte_array = C_string.getbytes ("UTF-8");
            if (c_byte_array.length>1) {chinesecount++;
            else {normalcount++;
    } System.out.println (Chinesecount+normalcount); } public static Boolean Ischinesechar (String STR) {if (str = null) return false;
    Boolean temp = false;
    Pattern p = pattern.compile (CHINESE_REG_EXP);
    Matcher m = p.matcher (str);
    if (M.find ()) {temp = true;
return to temp;
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.