private void Button_Click(object sender, RoutedEventArgs e)
{
bt_test.Content = GetStringFirstChar("啊比草第饿佛给好机可里米你哦屁去肉是他我想有在");
}
///<summary>
/// 获取字符串首字母
/// </summary>
public static string GetStringFirstChar(string str)
{
string returnStr = "";
if (string.IsNullOrEmpty(str))
{
return "";
}
byte[] array = new byte[2];
array = Encoding.Default.GetBytes(str);
for (int i = 0; i < array.Length; i+=2)
{
int j = (short)(array[i] - '\0') * 256 + ((short)(array[i + 1] - '\0'));
if (j < 0xB0A1)
{
returnStr += "";
}
else if (j < 0xB0C5)
{
returnStr += "a";
}
else if (j < 0xB2C1)
{
returnStr += "b";
}
else if (j < 0xB4EE)
{
returnStr += "c";
}
else if (j < 0xB6EA)
{
returnStr += "d";
}
else if (j < 0xB7A2)
{
returnStr += "e";
}
else if (j < 0xB8C1)
{
returnStr += "f";
}
else if (j < 0xB9FE)
{
returnStr += "g";
}
else if (j < 0xBBF7)
{
returnStr += "h";
}
else if (j < 0xBFA6)
{
returnStr += "j";
}
else if (j < 0xC0AC)
{
returnStr += "k";
}
else if (j < 0xC2E8)
{
returnStr += "l";
}
else if (j < 0xC4C3)
{
returnStr += "m";
}
else if (j < 0xC5B6)
{
returnStr += "n";
}
else if (j < 0xC5BE)
{
returnStr += "o";
}
else if (j < 0xC6DA)
{
returnStr += "p";
}
else if (j < 0xC8BB)
{
returnStr += "q";
}
else if (j < 0xC8F6)
{
returnStr += "r";
}
else if (j < 0xCBFA)
{
returnStr += "s";
}
else if (j < 0xCDDA)
{
returnStr += "t";
}
else if (j < 0xCEF4)
{
returnStr += "w";
}
else if (j < 0xD1B9)
{
returnStr += "x";
}
else if (j < 0xD4D1)
{
returnStr += "y";
}
else if (j < 0xD7FA)
{
returnStr += "z";
}
else
{
returnStr += "";
}
}
return returnStr;
}
点击按钮,效果实际如图,汉字首字母都获取到了