#region ### 得到首字母 方案1
public static string GetChineseFirstChar(string chineseStr)
{
StringBuilder sb = new StringBuilder();
int length = chineseStr.Length;
for (int i = 0; i < length; i++)
{
char chineseChar = chineseStr[i];
sb.Append(GetPYChar(chineseChar));
}
return sb.ToString();
}
private static string GetPYChar(char c)
{
//int ascCode = Microsoft.VisualBasic.Strings.Asc(c);
//int temp = 65536 + ascCode;
int temp = 65536 + GetAsciiCode(c);
//int temp = 65536 + Convert.ToInt16(c);
if (temp >= 45217 && temp <= 45252)
{
return "A";
}
else if (temp >= 45253 && temp <= 45760)
{
return "B";
}
else if (temp >= 45761 && temp <= 46317)
{
return "C";
}
else if (temp >= 46318 && temp <= 46825)
{
return "D";
}
else if (temp >= 46826 && temp <= 47009)
{
return "E";
}
else if (temp >= 47010 && temp <= 47296)
{
return "F";
}
else if (temp >= 47297 && temp <= 47613)
{
return "G";
}
else if (temp >= 47614 && temp <= 48118)
{
return "H";
}
else if (temp >= 48119 && temp <= 49061)
{
return "J";
}
else if (temp >= 49062 && temp <= 49323)
{
return "K";
}
else if (temp >= 49324 && temp <= 49895)
{
return "L";
}
else if (temp >= 49896 && temp <= 50370)
{
return "M";
}
else if (temp >= 50371 && temp <= 50613)
{
return "N";
}
else if (temp >= 50614 && temp <= 50621)
{
return "O";
}
else if (temp >= 50622 && temp <= 50905)
{
return "P";
}
else if (temp >= 50906 && temp <= 51386)
{
return "Q";
}
else if (temp >= 51387 && temp <= 51445)
{
return "R";
}
else if (temp >= 51446 && temp <= 52217)
{
return "S";
}
else if (temp >= 52218 && temp <= 52697)
{
return "T";
}
else if (temp >= 52698 && temp <= 52979)
{
return "W";
}
else if (temp >= 52980 && temp <= 53688)
{
return "X";
}
else if (temp >= 53689 && temp <= 54480)
{
return "Y";
}
else if (temp >= 54481 && temp <= 62289)
{
return "Z";
}
else
{
return c.ToString();
}
}
/// <summary>
/// 得到字符的ascii码
/// </summary>
private static int GetAsciiCode(char chr)
{
Encoding ecode = Encoding.GetEncoding("gb18030");
byte[] codebytes = ecode.GetBytes(chr.ToString());
if (IsTwoByteChar(chr))
{
// 双字节码为高位乘256,再加低位
// 该为无符号码,再减65536
return (int)codebytes[0] * 256 + (int)codebytes[1] - 65536;
}
else
{
return (int)codebytes[0];
}
}
/// <summary>
/// 是否为双字节字符。
/// </summary>
private static bool IsTwoByteChar(char chr)
{
string str = chr.ToString();
// 使用中文支持编码
Encoding ecode = Encoding.GetEncoding("gb18030");
if (ecode.GetByteCount(str) == 2)
{
return true;
}
else
{
return false;
}
}
#endregion
调用方法:
string ss = Common.GetChineseFirstChar("我a*%爱你中国"); //Wa*%ANZG
获取中文字符串拼音首字母

本文介绍了一种通过C#实现的方法,用于提取中文字符串中每个汉字对应的拼音首字母,并将其转换为大写英文字符。这种方法可以应用于各种场景,如中文姓名的拼音缩写生成等。文中提供了一个具体的示例,展示了如何将混合中文与非中文字符的字符串转换为首字母缩写。
1995

被折叠的 条评论
为什么被折叠?



