C # obtain the first letter of Chinese characters and pinyin,

Source: Internet
Author: User
Tags truncated

C # obtain the first letter of Chinese characters and pinyin,

I recently learned a truth, and I will share it with you here: Education represents your past, your present abilities represent your present, and learning represents your future.

Ten years in Hexi, do not bully teenagers

There is no end to learning, keep improving

This section describes how to obtain the first letter of Chinese pinyin in C:

Code:

/// <Summary> /// search for Chinese characters that match the pinyin index string in the specified string list CnStr /// </summary> /// <param name = "CnStr"> string </param> // <returns> corresponds to the first letter of Chinese pinyin </returns> public static string GetSpellCode (string CnStr) {string strTemp = ""; int iLen = CnStr. length; int I = 0; for (I = 0; I <= iLen-1; I ++) {strTemp + = GetCharSpellCode (CnStr. substring (I, 1); break;} return strTemp;} // <summary> // obtain the first letter of a Chinese character, if it is an English letter, return directly Uppercase letters // </summary> /// <param name = "CnChar"> single Chinese Character </param> /// <returns> single uppercase letter </returns> private static string GetCharSpellCode (string CnChar) {long iCnChar; byte [] ZW = System. text. encoding. default. getBytes (CnChar); // if it is a letter, the first letter if (ZW. length = 1) {return CommonMethod. cutString (CnChar. toUpper (), 1);} else {// get the array of byte from the single char int i1 = (short) (ZW [0]); int i2 = (sho Rt) (ZW [1]); iCnChar = i1 * 256 + i2;} // iCnChar match the constant if (iCnChar >=45217) & (iCnChar <= 45252) {return "A";} else if (iCnChar> = 45253) & (iCnChar <= 45760 )) {return "B";} else if (iCnChar >=45761) & (iCnChar <= 46317) {return "C ";} else if (iCnChar> = 46318) & (iCnChar <= 46825) {return "D";} else if (iCnChar> = 46826) & (iCnChar <= 47009) {return "E";} e Lse if (iCnChar> = 47010) & (iCnChar <= 47296) {return "F";} else if (iCnChar> = 47297) & (iCnChar <= 47613) {return "G";} else if (iCnChar> = 47614) & (iCnChar <= 48118 )) {return "H" ;}else if (iCnChar >=48119) & (iCnChar <= 49061) {return "J ";} else if (iCnChar> = 49062) & (iCnChar <= 49323) {return "K";} else if (iCnChar> = 49324) & (iCnChar <= 49895) {return "L ";} Else if (iCnChar> = 49896) & (iCnChar <= 50370) {return "M";} else if (iCnChar> = 50371) & (iCnChar <= 50613) {return "N";} else if (iCnChar> = 50614) & (iCnChar <= 50621 )) {return "O";} else if (iCnChar >=50622) & (iCnChar <= 50905) {return "P ";} else if (iCnChar> = 50906) & (iCnChar <= 51386) {return "Q";} else if (iCnChar> = 51387) & (iCnChar <= 51445) {return "R ";} Else if (iCnChar> = 51446) & (iCnChar <= 52217) {return "S";} else if (iCnChar> = 52218) & (iCnChar <= 52697) {return "T";} else if (iCnChar> = 52698) & (iCnChar <= 52979 )) {return "W";} else if (iCnChar >=52980) & (iCnChar <= 53640) {return "X ";} else if (iCnChar> = 53689) & (iCnChar <= 54480) {return "Y";} else if (iCnChar> = 54481) & (iCnChar <= 55289) {return "Z ";} Else return ("? ");}

String truncation method:

# Region truncated character length static string CutString (string str, int len) /// <summary> /// capture character length /// </summary> /// <param name = "str"> truncated string </param> // /<param name = "len"> intercepted length </param> // <returns> substring </returns> public static string CutString (string str, int len) {if (str = null | str. length = 0 | len <= 0) {return string. empty;} int l = str. length; # region calculation Length int clen = 0; while (clen <len & cle N <l) {// For each Chinese character, the target length is reduced by one. If (int) str [clen]> 128) {len --;} clen ++;} # endregion if (clen <l) {return str. substring (0, clen) + "... ";}else {return str ;}} /// <summary> //// capture Chinese characters in the string // </summary> /// <param name = "content"> source string </param>/ // <param name = "length"> truncation length! </Param> // <returns> </returns> public static string SubTrueString (object content, int length) {string strContent = NoHTML (content. toString (); bool isConvert = false; int splitLength = 0; int currLength = 0; int code = 0; int chfrom = Convert. toInt32 ("4e00", 16); // range (0x4e00 ~ 0x9fff) to int (chfrom ~ Chend) int chend = Convert. toInt32 ("9fff", 16); for (int I = 0; I <strContent. length; I ++) {code = Char. convertToUtf32 (strContent, I); if (code> = chfrom & code <= chend) {currLength + = 2; // Chinese} else {currLength + = 1; // non-Chinese} splitLength = I + 1; if (currLength> = length) {isConvert = true; break ;}} if (isConvert) {return strContent. substring (0, splitLength);} else {return strContent ;}} Public static int GetStringLenth (object content) {string strContent = NoHTML (content. toString (); int currLength = 0; int code = 0; int chfrom = Convert. toInt32 ("4e00", 16); // range (0x4e00 ~ 0x9fff) to int (chfrom ~ Chend) int chend = Convert. toInt32 ("9fff", 16); for (int I = 0; I <strContent. length; I ++) {code = Char. convertToUtf32 (strContent, I); if (code> = chfrom & code <= chend) {currLength + = 2; // Chinese} else {currLength + = 1; // non-Chinese} return currLength;} # endregion

The above is the complete code. Thank you!

Here, by the way, the database is sorted by the first letter of Chinese characters:

Oracle:

The sorting by pinyin, radical, and stroke function is added to oracle9i. Set the NLS_SORT Value
SCHINESE_RADICAL_M is sorted by the beginning (first order) and strokes (Second Order ).
SCHINESE_STROKE_M is sorted by strokes (first order) and heads (Second Order ).
SCHINESE_PINYIN_M is sorted by pinyin. The default sorting method is pinyin.

Example:
The table name is dept, where the name field is Chinese. The following lists the strokes, radicals, and Pinyin of the unit names.
// Sort by strokes
Select * from dept order by nlssort (name, 'nls _ SORT = SCHINESE_STROKE_M ');
// Sort by department heads
Select * from dept order by nlssort (name, 'nls _ SORT = SCHINESE_RADICAL_M ');
// Sort by pinyin. This is the default sorting method of the system.
Select * from dept order by nlssort (name, 'nls _ SORT = SCHINESE_PINYIN_M ');

Sqlserver

Select * from table order by name collate Chinese_PRC_CS_AS_KS_WS 

Execution result: select * from Table where cateFid = 0 order by cateName collate Chinese_PRC_CS_AS_KS_WS

@ Chen Wolong's blog

Related Article

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.