Use the upper-case letters of Chinese characters and Pinyin

Source: Internet
Author: User

The following code obtains the upper-case letters starting with each letter in Chinese pinyin.

Public class gb2alpha {</P> <p> // two tags are used for Z. There are 27 values <br/> // I, U, and V are not used as the mother sound, follow the preceding letter <br/> private char [] chartable = <br/> {<br/> 'Ah', 'bar', 'ru', 'Ride ', 'Moth ', 'fa', 'taobao', 'ha ', 'ha', <br/> 'click', 'CA', 'taobao', 'Ma ', 'A', 'Oh', 'cl', 'stance', 'ran', <br/> 'za', 'collapsing ', 'dig', 'seg', 'stopped', 'zac', 'block' <br/> }; </P> <p> private char [] alphatable = <br/> {<br/> 'A', 'B', 'C', 'D ', 'E', 'F', 'G', 'h', 'I', <br/> 'J', 'k', 'l', 'M ', 'N', 'O', 'P', 'Q', 'R', <br/> 's', 't', 'U', 'V ', 'w', 'x', 'y', 'z' <br/>}; </P> <p> private int [] Table = new int [27]; <br/> // initialization <br/> {<br/> for (INT I = 0; I <27; ++ I) {<br/> table [I] = gbvalue (chartable [I]); <br/>}</P> <p> Public gb2alpha () {<br/>}</P> <p> // main function. Enter a character to obtain its initials, <br/> // return uppercase letters <br/> // return '0' for other non-simplified Chinese characters </P> <p> Public char char2alpha (char ch) {</P> <p> If (CH> = 'A' & Ch <= 'Z') <br/> return (char) (CH-'A' + 'A'); <br/> If (CH> = 'A' & Ch <= 'Z') <br/> return ch; <br/> int GB = gbvalue (CH); <br/> If (GB <Table [0]) <br/> return '0 '; <br/> int I; <br/> for (I = 0; I <26; ++ I) {<br/> If (MATCH (I, GB )) break; <br/>}< br/> if (I> = 26) <br/> return '0 '; <br/> else <br/> return alphatable [I]; <br/>}</P> <p> // returns the first letter of a Chinese character string based on a string containing Chinese characters. <br/> Public String string2alpha (string sourcestr) {</P> <p> string result = ""; <br/> int strlength = sourcestr. length (); <br/> int I; <br/> try {<br/> for (I = 0; I <strlength; I ++) {<br/> result + = char2alpha (sourcestr. charat (I); <br/>}< br/>} catch (exception e) {<br/> result = ""; <br/>}< br/> return result; <br/>}</P> <p> private Boolean match (int I, int GB) {</P> <p> If (GB <Table [I]) <br/> return false; <br/> Int J = I + 1; </P> <p> // two tags are used for Z. <br/> while (j <26 & (Table [J] = table [I]) + + J; <br/> If (j = 26) <br/> return GB <= table [J]; <br/> else <br/> return GB <Table [J]; <br/>}</P> <p> // extract the Chinese character encoding <br/> private int gbvalue (char ch) {</P> <p> string STR = new string (); <br/> STR + = CH; <br/> try {<br/> byte [] bytes = Str. getbytes ("gb2312"); <br/> If (bytes. length <2) <br/> return 0; <br/> return (Bytes [0] <8 & 0xff00) + (Bytes [1] & <br/> 0xff); <br/>} catch (exception e) {<br/> return 0; <br/>}</P> <p> Public static void main (string [] ARGs) {</P> <p> gb2alpha obj1 = new gb2alpha (); <br/> system. out. println (obj1.string2alpha (" csdn"); <br/> system. out. println (obj1.string2alpha ("learning hard"); <br/> return; <br/>}< br/>}

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.