/// <summary> /// 汉字转换 /// </summary> public class ChineseConvert { #region 汉字转拼方法1 /// <summary>汉字转拼音缩写</summary> /// <param name="str">要转换的汉字字符串</param> /// <returns>拼音缩写</returns> public static string GetSpellString(string str) { if (IsEnglishCharacter(str)) return str; string tempStr = ""; foreach (char c in str) { if ((int)c >= 33 && (int)c <= 126) {//字母和符号原样保留 tempStr += c.ToString(); } else {//累加拼音声母 tempStr += GetSpellChar(c.ToString()); } } return tempStr; } /// <summary> /// 判断是否字母 /// </summary> /// <param name="str"></param> /// <returns></returns> private static bool IsEnglishCharacter(String str) { CharEnumerator ce = str.GetEnumerator(); while (ce.MoveNext()) { char c = ce.Current; if ((c <= 0x007A && c >= 0x0061) == false && (c <= 0x005A && c >= 0x0041) == false) return false; } return true; } /// <summary> /// 判断是否有汉字 /// </summary> /// <param name="testString"></param> /// <returns></returns> private static bool HaveChineseCode(string testString) { if (Regex.IsMatch(testString, @"[u4e00-u9fa5]+")) { return true; } else { return false; } } /// <summary> /// 取单个字符的拼音声母 /// </summary> /// <param name="c">要转换的单个汉字</param> /// <returns>拼音声母</returns> public static string GetSpellChar(string c) { byte[] array = new byte[2]; array = System.Text.Encoding.Default.GetBytes(c); int i = (short)(array[0] - '