/// <summary> ///gets the string first character letter (uppercase);/// </summary> /// <param name= "Cnchar" ></param> /// <returns></returns> Public stringGetstringfirstspell (stringCnchar) { //return "*" except for letters, numbers, and kanji varresult ="*"; if(string. IsNullOrEmpty (Cnchar.trim ()))returnresult; Cnchar= Cnchar.trim (). Substring (0,1); byte[] Arrcn =Encoding.Default.GetBytes (Cnchar); //The first word is a character and takes up one byte if(Arrcn.length <=1) { //Uppercase English Letters if(( Short) arrcn[0] >= $&& ( Short) arrcn[0] <= -) returnCnchar; //Lowercase English letters if(arrcn[0] >= the&& arrcn[0] <=122) returnEncoding.Default.GetString (New byte[] { (byte)(( Short) arrcn[0] - +) }); //Digital Switch(Cnchar) { Case "1": Result="Y"; Break; Case "2": Result="E"; Break; Case "3": Case "4": Result="S"; Break; Case "5": Result="W"; Break; Case "0": Case "6": Result="L"; Break; Case "7": Result="Q"; Break; Case "8": Result="B"; Break; Case "9": Result="J"; Break; } returnresult; } //The first character is a Chinese character, accounting for two bytes Else { intArea = ( Short) arrcn[0]; intpos = ( Short) arrcn[1]; intCode = (Area <<8) +POS; int[] AreaCode = {45217,45253,45761,46318,46826,47010,47297,47614,48119,48119,49062,49324,49896,50371,50614,50622,50906,51387,51446,52218,52698,52698,52698,52980,53689,54481 }; for(inti =0; I < -; i++) { intMax =55290; if(I! = -) max = Areacode[i +1]; if(Areacode[i] <= code && Code <max) { returnEncoding.Default.GetString (New byte[] { (byte)( $+i)}); } } returnresult; } }
Sometimes it is necessary to obtain the initials of Chinese characters, which can be used in the above method.
C # get string first character letter (uppercase)