C # obtain the first subtitle and full spelling of Chinese characters and pinyin (Traditional Chinese characters are not supported)

Source: Internet
Author: User

Public class ChineseCode
{
Protected string _ CnTxt;
Protected string _ EnTxt;

///
/// Chinese characters for letters
///
Public string CnTxt
{
Get {return _ CnTxt ;}
Set {_ CnTxt = value ;}
}
///
/// The first letter of a Chinese character
///
Public string EnTxt
{
Get {return _ EnTxt ;}
Set {_ EnTxt = value ;}
}
///
/// Constructor
///
Public ChineseCode ()
{

}
///
/// Constructor
///
///
Public ChineseCode (string txt)
{
_ CnTxt = txt;
_ EnTxt = IndexCode (_ CnTxt );
}

/// <Summary>
/// Obtain the first letter of a Chinese character
/// </Summary>
/// <Param name = "IndexTxt"> Chinese characters </param>
/// <Returns> first letter of a Chinese character </returns>
Public String IndexCode (String IndexTxt)
{
String _ Temp = null;
For (int I = 0; I <IndexTxt. Length; I ++)
_ Temp = _ Temp + GetOneIndex (IndexTxt. Substring (I, 1 ));
Return _ Temp;
}

/// <Summary>
/// Single Chinese Character
/// </Summary>
/// <Param name = "OneIndexTxt"> Chinese Character </param>
/// <Returns> first fight </returns>
Private String GetOneIndex (String OneIndexTxt)
{
If (Convert. ToChar (OneIndexTxt)> = 0 & Convert. ToChar (OneIndexTxt) <256)
Return OneIndexTxt;
Else
{
Encoding gb2312 = Encoding. GetEncoding ("gb2312 ");
Byte [] unicodeBytes = Encoding. Unicode. GetBytes (OneIndexTxt );
Byte [] gb2312Bytes = Encoding. Convert (Encoding. Unicode, gb2312, unicodeBytes );
Return GetX (Convert. ToInt32 (
String. Format ("{0: D2}", Convert. ToInt16 (gb2312Bytes [0])-160)
+ String. Format ("{0: D2}", Convert. ToInt16 (gb2312Bytes [1])-160)
));
}
}

/// <Summary>
/// Obtain the first letter Based on the location
/// </Summary>
/// <Param name = "GBCode"> location </param>
/// <Returns> initial letter </returns>
Private String GetX (int GBCode)
{
If (GBCode> = 1601 & GBCode <1637) return "";
If (GBCode> = 1637 & GBCode <1833) return "B ";
If (GBCode> = 1833 & GBCode <2078) return "C ";
If (GBCode> = 2078 & GBCode <2274) return "D ";
If (GBCode> = 2274 & GBCode <2302) return "E ";
If (GBCode> = 2302 & GBCode <2433) return "F ";
If (GBCode> = 2433 & GBCode <2594) return "G ";
If (GBCode> = 2594 & GBCode <2787) return "H ";
If (GBCode> = 2787 & GBCode <3106) return "J ";
If (GBCode> = 3106 & GBCode <3212) return "K ";
If (GBCode> = 3212 & GBCode <3472) return "L ";
If (GBCode> = 3472 & GBCode <3635) return "M ";
If

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.