/// <summary>
/// 指定汉字字符串中检索符合拼音索引字符串
/// </summary>
/// <param name="chineseCharacters">汉字字符串</param>
/// <param name="isControlCharacter">是否返回控制字符</param>
/// <returns>汉语拼音相对应首字母集合</returns>
public static List<(string, string)> GetChineseSpellCode(List<string> chineseCharacters, bool isControlCharacter = false)
{
var result = new List<(string, string)>();
try
{
foreach (var tempString in chineseCharacters)
{
var tempItem2 = string.Empty;
foreach (var tempItem1 in tempString)
{
tempItem2 += GetChineseLetter(tempItem1.ToString(), isControlCharacter);
}
result.Add((tempString, tempItem2));
}
}
catch { }
return result;
}
/// <summary>
/// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
/// </summary>
/// <param name="chineseCharacters">单个汉字</param>
/// <param name="isControlCharacter">是否返回控制字符</param>
/// <returns>单个大写字母</returns>
private static string GetChineseLetter(string chineseCharacters, bool isControlCharacter = false)
{
var result = string.Empty;
try
{
if (string.IsNullOrEmpty(chineseCharacters))
{
return result;
}
else if (chineseCharacters.Length > 1)
{
chineseCharacters = chineseCharacters.Substring(0, 1);
}
var tempArray = System.Text.Encoding.Default.GetBytes(chineseCharacters);
if (tempArray.Length == 1)
{
var tempChar = (int)chineseCharacters[0];
if (isControlCharacter)
{
/// 0~127[33个控制字符,95个可显示字符]
if (tempChar < 128)
{
result = chineseCharacters.ToUpper();
}
}
else
{
/// 数字/大写字母/小写字母
if ((tempChar >= 48 && tempChar < 57) || (tempChar >= 65 && tempChar <= 90) || (tempChar >= 97 && tempChar <= 122))
{
result = chineseCharacters.ToUpper();
}
}
}
else
{
int value1 = (short)(tempArray[0]);
int value2 = (short)(tempArray[1]);
long charValue = value1 * 256 + value2;
if ((charValue >= 45217) && (charValue <= 45252))
{
result = "A";
}
else if ((charValue >= 45253) && (charValue <= 45760))
{
result = "B";
}
else if ((charValue >= 45761) && (charValue <= 46317))
{
result = "C";
}
else if ((charValue >= 46318) && (charValue <= 46825))
{
result = "D";
}
else if ((charValue >= 46826) && (charValue <= 47009))
{
result = "E";
}
else if ((charValue >= 47010) && (charValue <= 47296))
{
result = "F";
}
else if ((charValue >= 47297) && (charValue <= 47613))
{
result = "G";
}
else if ((charValue >= 47614) && (charValue <= 48118))
{
result = "H";
}
else if ((charValue >= 48119) && (charValue <= 49061))
{
result = "J";
}
else if ((charValue >= 49062) && (charValue <= 49323))
{
result = "K";
}
else if ((charValue >= 49324) && (charValue <= 49895))
{
result = "L";
}
else if ((charValue >= 49896) && (charValue <= 50370))
{
result = "M";
}
else if ((charValue >= 50371) && (charValue <= 50613))
{
result = "N";
}
else if ((charValue >= 50614) && (charValue <= 50621))
{
result = "O";
}
else if ((charValue >= 50622) && (charValue <= 50905))
{
result = "P";
}
else if ((charValue >= 50906) && (charValue <= 51386))
{
result = "Q";
}
else if ((charValue >= 51387) && (charValue <= 51445))
{
result = "R";
}
else if ((charValue >= 51446) && (charValue <= 52217))
{
result = "S";
}
else if ((charValue >= 52218) && (charValue <= 52697))
{
result = "T";
}
else if ((charValue >= 52698) && (charValue <= 52979))
{
result = "W";
}
else if ((charValue >= 52980) && (charValue <= 53640))
{
result = "X";
}
else if ((charValue >= 53689) && (charValue <= 54480))
{
result = "Y";
}
else if ((charValue >= 54481) && (charValue <= 55289))
{
result = "Z";
}
}
}
catch { }
return result;
}
指定汉字字符串中检索符合拼音索引字符串
最新推荐文章于 2024-07-04 00:52:37 发布