Java get the first letter of Chinese pinyin-reproduced

Source: Internet
Author: User

In the project to be more able to according to some query criteria (such as name) of the first letter as a condition to query, such as check a person called "Li Xiaoming", you can enter ' LXM '. A tool class was written as follows:

Import java.io.UnsupportedEncodingException;

  • /**
  • * Get the first letter string of a given Chinese character string, that is, the initials string
  • * Title:chinesechartoen
  • * @date 2004-02-19 Note: Only Chinese characters in the GB2312 character set are supported
  • */
  • Public Final 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
  • */
    1. 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;
  • }
  • /**
  • * Get the first letter of a given Chinese character, i.e. initials
  • * @param Chinese the given kanji
  • * @return the initials of a 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); //Kanji Area code
  • int li_positioncode = (int) Chinese.charat (1); //Kanji 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 First letter:" + cte.getallfirstletter ("Beijing Joint Office"));
  • }
  • }

Blog Original: http://blog.csdn.net/fei1502816/article/details/8446049/

Java get the first letter of Chinese pinyin-reproduced

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.