How to take the first letter of Chinese pinyin in Java __java

Source: Internet
Author: User

Description: A Java class used to take the first letter of Chinese pinyin, for example: The Chinese character "People's Republic of China" takes the Chinese pinyin first letter the result is ZHRMGHG.

The procedure is as follows:

Package GOV.MCA;

/**
* Created by IntelliJ idea.
* User: Pei Hexian
* DATE:2004-5-17
* time:10:59:59
* Classdescription: Remove the phonetic initials of the Chinese character string
*/

Import java.lang.*;

public class Gb2alpha {

The letter Z uses two tags, here are 27 values
I, u, v do not make initials, follow the letters in front
Private char[] Chartable =
{
' Ah ', ' ba ', ' Rub ', ' Ride ', ' moth ', ' hair ', ' karma ', ' ha ', ' ha ',
' Strike ', ' ka ', ' garbage ', ' Mom ', ' take ', ' Oh ', ' pop ', ' time ', ' yes ',
' Satan ', ' collapse ', ' collapse ', ' collapse ', ' dig ', ' Shake ', ' press ', ' turn ', ' seat '
};

Private char[] Alphatable =
{
' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ',

' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '
};


Private int[] table = new INT[27];

Class
{
for (int i = 0; i < ++i) {
Table[i] = Gbvalue (Chartable[i]);
}
}

Public Gb2alpha () {

}

The main function, the input character, gets his initials,
English letters return the corresponding uppercase letters
Other non-Simplified Chinese characters return ' 0 '

Public char char2alpha (char ch) {

if (Ch >= ' a ' && ch <= ' z ')
Return (char) (CH-' a ' + ' a ');
if (ch >= ' A ' && ch <= ' Z ')
return ch;


int GB = Gbvalue (CH);
if (GB < table[0])
Return ' 0 ';


int i;
for (i = 0; i < ++i) {
if (Match (I, GB))
Break
}

        if (i >=)
             return ' 0 ';
        Else
             return alphatable[i];
   }

   //Returns a string of Chinese pinyin first letter based on a string containing Chinese characters
    public string String2alpha (string SOURCESTR) {
        String result = ';
        int strlength = Sourcestr.length ();
        int i;
        try {
             for (i = 0; i < strlength i++) {
                 result + = Char2alpha (Sourcestr.charat (i));
           }
       } catch (Exception e) {
             result = "";
       }
        return result;
   }

Private Boolean match (int i, int GB) {
if (GB < table[i])
return false;

Int J = i + 1;

The letter Z uses two labels
while (J < && (Table[j] = = Table[i])
++j;

if (j = = 26)
return GB <= Table[j];
Else
return GB < TABLE[J];

}

To remove the encoding of Chinese characters
private int Gbvalue (char ch) {
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) {
Com.mkrx.GB2Alpha obj1 = new Gb2alpha ();
System.out.println (obj1. String2alpha ("Test": People's Republic of China. "));
System.out.println (obj1. String2alpha ("Pei Hexian"));
Return
}
}

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.