Java gets the first letter of Chinese characters

Source: Internet
Author: User


Package com.pinyin;  
    The public class Pinyinutil {//Simplified Chinese encoding range from B0A1 (45217) until F7fe (63486) private static int BEGIN = 45217;  
  
    private static int end = 63486;  
    According to the initials, the table is the first Chinese character to appear in the GB2312, that is to say "Ah" is the first character representing the first letter A. I, u, v do not make initials, custom rules follow the letter of the previous private static char[] chartable = {' Ah ', ' ba ', ' rub ', ' get ', ' moth ', ' hair ', ' karma ', ' ha ', ' ha ', ' hit ', '  
  
    Kerala, ' garbage ', ' Mom ', ' take ', ' Oh ', ' pop ', ' time ', ' yes ', ' Satan ', ' collapse ', ' collapse ', ' collapse ', ' dig ', ' Shake ', ' press ', ' Turn ',};  
  
    26 letter interval corresponding to 27 endpoints//GB2312 code Chinese character interval decimal means private static int[] table = new INT[27]; Corresponding first-letter interval table private static char[] initialtable = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' H ', ' J ', ' K ', ' l ', ' m ',  
  
    ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' t ', ' t ', ' W ', ' x ', ' y ', ' z ',}; Initialize static {for (int i = 0; i < i++) {Table[i] = Gbvalue (chartable[i));/Get GB23  
        12-yard first-letter interval endpoint table, decimal. } table[26] = end;//interval table End}//------------------------public method Area------------------------//According to a string containing Chinese characters, the most important method is to return a character string with the first letter of Chinese pinyin, as follows: A character reads, judges, and outputs the public static string Cn2py (Stri  
        ng Sourcestr) {String result = "";  
        int strlength = Sourcestr.length ();  
        int i;  
            try {for (i = 0; i < strlength i++) {result = Char2initial (Sourcestr.charat (i));  
            The catch (Exception e) {result = "";  
        E.printstacktrace ();  
    return result; }//------------------------Private Method Area------------------------/** * Input character, get his initials, English letters return corresponding uppercase letters, other non-simplified  
        Chinese characters return ' 0 ' * */private static char char2initial (char ch) {//Letter processing: Lowercase letters converted to uppercase, uppercase direct return  
        if (Ch >= ' a ' && ch <= ' Z ') {return (char) (CH-' a ' + ' a ');  
        } if (ch >= ' A ' && ch <= ' Z ') {return ch; //non-English alphabet processing: Converts the head letter and then determines whether it is within the range of the Code table, 
        If not, return directly.  
        If, then in the Code table to judge. int GB = Gbvalue (ch);//Kanji Conversion First Letter if (GB < BEGIN) | | (GB > End))  
        Returns the {return ch directly before the code table interval;  
        } int i; for (i = 0; i < i++) {//Judge match Code table interval, match to on break, Judge interval form as "[,)" if ((GB >= table[i]) && (GB < T  
            Able[i + 1]) {break;  
        } if (GB = = end) {//GB2312 the most right of the interval i = 25; return initialtable[i]; In the Code table interval, return the first letter}/** * The encoding of the Chinese character cn/private static int gbvalue (char ch) {//will be a Chinese character  
        (GB2312) is converted to decimal notation.  
        String str = new string ();  
        STR + ch;  
            try {byte[] bytes = str.getbytes ("GB2312");  
            if (Bytes.length < 2) {return 0;  
        Return (Bytes[0] << 8 & 0xff00) + (Bytes[1] & 0xff); catch (Exception e){return 0; } public static void Main (string[] args) throws Exception {System.out.println ("Chongqing attaches importance to developing it lines  
    Industry, most foreign companies, such as, IBM, such as stationed in the Mountain City "));
 }  
}


Original address: http://itlab.idcquan.com/Java/base/803353.html



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.