Commit d943ccda authored by tr's avatar tr

1、工具类:汉字拼音缩写生成

parent 8845f191
...@@ -157,7 +157,7 @@ namespace Tools.MyUtils ...@@ -157,7 +157,7 @@ namespace Tools.MyUtils
} }
#endregion #endregion
#region DataTable増行扩展 #region (扩展)DataTable増行
/// <summary> /// <summary>
/// 【自定义扩展】获取一个新行 /// 【自定义扩展】获取一个新行
...@@ -224,6 +224,21 @@ namespace Tools.MyUtils ...@@ -224,6 +224,21 @@ namespace Tools.MyUtils
return result; return result;
} }
#endregion
#region (扩展)整型范围
/// <summary>
/// 整数是否在指定范围内
/// </summary>
/// <param name="a">整数</param>
/// <param name="low">下边界</param>
/// <param name="high">上边界</param>
/// <returns></returns>
public static bool Between(this int a, int low, int high)
{
return a <= high && a >= low;
}
#endregion #endregion
#region 生成Model #region 生成Model
...@@ -342,5 +357,65 @@ namespace Tools.MyUtils ...@@ -342,5 +357,65 @@ namespace Tools.MyUtils
#endregion #endregion
#region 汉字拼音缩写
/// <summary>
/// 获取汉字拼音缩写
/// </summary>
/// <param name="words"></param>
/// <returns></returns>
public static string GetPinyinString(string words)
{
char[] buffer = new char[words.Length];
for (int i = 0; i < words.Length; i++)
{
buffer[i] = GetPinyinChar(words[i]);
}
return new string(buffer);
}
///<summary>
/// 获取拼音首字母
///</summary>
///<param name="word">一个汉字</param>
/// <returns></returns>
private static char GetPinyinChar(char word)
{
//如果不是汉字直接返回该字符
byte[] unibytes = Encoding.Unicode.GetBytes(new char[] { word });
if (unibytes[1] == 0)
return word;
//将Unicode转为国标码
byte[] gbytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding("GB2312"), unibytes);
//转为汉字域码:高字节左移8位,加上低位字节
int n = (gbytes[0] << 8) + gbytes[1];
//根据汉字域码获取拼音首字母
if (n.Between(0xB0A1, 0xB0C4)) return 'a';
if (n.Between(0XB0C5, 0XB2C0)) return 'b';
if (n.Between(0xB2C1, 0xB4ED)) return 'c';
if (n.Between(0xB4EE, 0xB6E9)) return 'd';
if (n.Between(0xB6EA, 0xB7A1)) return 'e';
if (n.Between(0xB7A2, 0xB8c0)) return 'f';
if (n.Between(0xB8C1, 0xB9FD)) return 'g';
if (n.Between(0xB9FE, 0xBBF6)) return 'h';
if (n.Between(0xBBF7, 0xBFA5)) return 'j';
if (n.Between(0xBFA6, 0xC0AB)) return 'k';
if (n.Between(0xC0AC, 0xC2E7)) return 'l';
if (n.Between(0xC2E8, 0xC4C2)) return 'm';
if (n.Between(0xC4C3, 0xC5B5)) return 'n';
if (n.Between(0xC5B6, 0xC5BD)) return 'o';
if (n.Between(0xC5BE, 0xC6D9)) return 'p';
if (n.Between(0xC6DA, 0xC8BA)) return 'q';
if (n.Between(0xC8BB, 0xC8F5)) return 'r';
if (n.Between(0xC8F6, 0xCBF9)) return 's';
if (n.Between(0xCBFA, 0xCDD9)) return 't';
if (n.Between(0xCDDA, 0xCEF3)) return 'w';
if (n.Between(0xCEF4, 0xD188)) return 'x';
if (n.Between(0xD1B9, 0xD4D0)) return 'y';
if (n.Between(0xD4D1, 0xD7F9)) return 'z';
return '\0';
}
#endregion
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment