Chinese first letter

Source: Internet
Author: User

public class Chinesechartoen {

Private final static int[] Li_secposvalue = {1601, 1637, 1833, 2078, 2274,
2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590};
Private final static string[] Lc_firstletter = {"A", "B", "C", "D", "E",
"F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s",
"T", "W", "X", "Y", "Z"};

/**
* Get the first letter string of a given Chinese character string, that is, the initials string
* @param str Given string of Chinese characters
* @return Initials STRING
*/
public string Getallfirstletter (String str) {
if (str = = NULL | | Str.trim (). Length () = = 0) {
Return "";
}

String _str = "";
for (int i = 0; i < str.length (); i++) {
_str = _str + this.getfirstletter (str.substring (i, i + 1));
}

return _str;
}

/**
* Gets the first letter of the given Chinese character, that is, the initials
* @param Chinese the given Kanji
* @return The initials of the given Chinese character
/
public String Getfirstletter (String Chinese) {
if (Chinese = = NULL | | Chinese.trim (). Length () = = 0) {
return "";
}
Chinese = this.conversionstr (Chinese, "GB2312", "iso8859-1");

if (Chinese.length () > 1)//judgment is not kanji
{
int li_sectorcode = (int) chinese.charat (0); Chinese character Area code
int li_positioncode = (int) chinese.charat (1); Chinese character bit code
Li_sectorcode = li_sectorcode-160;
Li_positioncode = li_positioncode-160;
int Li_secposcode = Li_sectorcode * + li_positioncode; Chinese character Location Code
if (Li_secposcode > && Li_secposcode < 5590) {
for (int i = 0; i < i++) {
if (Li_secposcode >= li_secposvalue[i]
&& Li_secposcode < Li_secposvalue[i + 1]) {
Chinese = lc_firstletter[i];
Break
}
}
} else//non-kanji character, form symbol or ASCII code
{
Chinese = this.conversionstr (Chinese, "iso8859-1", "GB2312");
Chinese = chinese.substring (0, 1);
}
}

return Chinese;
}

/**
* String Encoding Conversion
* @param STR to convert the encoded string
* @param charsetname The original code
* @param tocharsetname converted Encoding
* @return The converted string after encoding
*/
private string Conversionstr (String str, string charsetname,string tocharsetname) {
try {
str = new String (Str.getbytes (CharsetName), tocharsetname);
} catch (Unsupportedencodingexception ex) {
SYSTEM.OUT.PRINTLN ("String encoding Conversion Exception:" + Ex.getmessage ());
}
return str;
}

public static void Main (string[] args) {
Chinesechartoen CTE = new Chinesechartoen ();
System.out.println ("Get Pinyin initials:" + Cte.getallfirstletter ("West Glass Village Committee"));
}

}

Chinese first letter

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.