. NET Common skills _ kanji to pinyin class (except Chinese characters, the rest of the original output)

Source: Internet
Author: User

Note: If it is polyphone, the first pinyin is taken by default.

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingMicrosoft.International.Converters.PinYinConverter;namespacemytool{/// <summary>    ///Chinese characters to Pinyin class/// </summary>     Public classPinyinconver { PublicPinyinconver () {//            //TODO: Add constructor logic here//        }        #regionCustom Methods/// <summary>         ///converts a string into the first phonetic/// </summary>         /// <param name= "Chinesestr" >string</param>         /// <returns></returns>         Private Static stringConverttofirstpinyin (stringchinesestr) {            if(Chinesestr = =NULL)                return ""; Char[] Chararray =Chinesestr.tochararray (); StringBuilder SB=NewStringBuilder (); foreach(CharCinchChararray) {Chinesechar Chinesechar=NewChinesechar (c); System.Collections.ObjectModel.ReadOnlyCollection<string> Pycoll =Chinesechar.pinyins; foreach(stringPyinchpycoll) {                    if(Py! =NULL) {sb. Append (py. Substring (0,1));  Break; }                }            }            returnsb.        ToString (); }        /// <summary>         ///converts a string to pinyin (if the first pronunciation is encountered Polyphone)/// </summary>         /// <param name= "Chinesestr" >Kanji String</param>         /// <returns></returns>         Private Static stringConverttopinyin (stringchinesestr) {            if(Chinesestr = =NULL)                Throw NewArgumentNullException ("Chinesestr"); Char[] Chararray =Chinesestr.tochararray (); StringBuilder SB=NewStringBuilder (); List<string> Listpinyin =Newlist<string>(); foreach(CharCinchChararray) {Chinesechar Chinesechar=NewChinesechar (c); System.Collections.ObjectModel.ReadOnlyCollection<string> Pycoll =Chinesechar.pinyins; if(pycoll[0] ==NULL)                    Continue; stringStrpinyin = pycoll[0]. Substring (0, pycoll[0]. Length-1); if(!listpinyin.contains (Strpinyin)) sb.                Append (Strpinyin);            Listpinyin.add (Strpinyin); }            returnsb.        ToString (); }         Public Static BOOLIsvalidchar (Charch) {            returnChinesechar.isvalidchar (CH); }        /// <summary>         ///Is a valid Chinese word/// </summary>         /// <param name= "CHN" ></param>         /// <returns></returns>          Public Static BOOLIsvalidchinese (stringCHN) {            if(CHN = =NULL)                return false; foreach(CharCinchCHN) {                if(!Isvalidchar (c))return false; }            return true; }        #endregion        /// <summary>        ///Generate help Codes/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>         Public Static stringCreatennemoniccode (stringstrName) {            stringStrpin =StrName; if(string. IsNullOrEmpty (strName) = =false)            {                stringText =StrName; Char[] C =text.                ToCharArray (); stringStrsum ="";  for(inti =0; i < c.length; i++)                {                    //whether it is kanji                    if(C[i] >=0x4e00&& C[i] <=0X9FBB)                    {                        stringstrtemp =""; strtemp=C[i].                        ToString (); strtemp=MyTool.PinYinConver.ConvertToFirstPinYin (strtemp); Strsum+=Strtemp.toupper (); }                    Else if(Char.isupper (c[i]) | |Char.islower (C[i])) {                        //if it's uppercase and lowercase,Strsum + =C[i]. ToString ().                    ToUpper (); }                    Else if(! Char.isupper (C[i]) &&! Char.islower (C[i]) &&! ((C[i] >=0x4e00&& C[i] <=0X9FBB)))                    {                        //other as isStrsum + =C[i].                    ToString (); }} Strpin=strsum; //Strpin = Strsum.toupper ();            }            Else{Strpin=""; }            returnStrpin; }        /// <summary>        ///Generate phonetic Codes/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>         Public Static stringCreatespellcode (stringstrName) {            stringStrpin =StrName; if(string. IsNullOrEmpty (strName) = =false)            {                stringText =StrName; Char[] C =text.                ToCharArray (); stringStrsum ="";  for(inti =0; i < c.length; i++)                {                    //whether it is kanji                    if(C[i] >=0x4e00&& C[i] <=0X9FBB)                    {                        stringstrtemp =""; strtemp=C[i].                        ToString (); strtemp=MyTool.PinYinConver.ConvertToPinYin (strtemp); Strsum+=Strtemp.tolower (); }                    Else if(Char.isupper (c[i]) | |Char.islower (C[i])) {                        //if it's uppercase and lowercase,Strsum + =C[i]. ToString ().                    ToLower (); }                    Else if(! Char.isupper (C[i]) &&! Char.islower (C[i]) &&! ((C[i] >=0x4e00&& C[i] <=0X9FBB)))                    {                        //other as isStrsum + =C[i].                    ToString (); }} Strpin=strsum; //Strpin = Strsum.tolower ();            }            Else{Strpin=""; }            returnStrpin; }    }}

Call-time Example:
Note: It is best to write in the KeyUp event when calling

private void Txtmaterialname_keyup (object sender, KeyEventArgs e)
{

Incoming string or text box content to be processed, generate a mnemonic
This.txtNnemonicCode.Text = MyTool.PinYinConver.CreateNnemonicCode (This.txtMaterialName.Text);

Incoming string or text box content to be processed, generate phonetic code
This.txtSpellCode.Text = MyTool.PinYinConver.CreateSpellCode (This.txtMaterialName.Text);
}

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.