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);
}