zoukankan      html  css  js  c++  java
  • C#根据汉字生成拼音首字母全称

     1 static void Main(string[] args)
     2 {
     3     string s = GetChineseSpell("周杰伦");
     4     Console.WriteLine(s.ToLower());
     5     Console.ReadKey();
     6 }
     7 static public string GetChineseSpell(string strText)
     8 {
     9     int len = strText.Length;
    10     string myStr = "";
    11     for (int i = 0; i < len; i++)
    12     {
    13     myStr += getSpell(strText.Substring(i, 1));
    14     }
    15     return myStr;
    16 }
    17 
    18 static public string getSpell(string cnChar)
    19 {
    20     byte[] arrCN = Encoding.Default.GetBytes(cnChar);
    21     if (arrCN.Length > 1)
    22     {
    23     int area = (short)arrCN[0];
    24     int pos = (short)arrCN[1];
    25     int code = (area << 8) + pos;
    26     int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 
    27   };
    28 for (int i = 0; i < 26; i++)
    29 {
    30     int max = 55290;
    31     if (i != 25) max = areacode[i + 1];
    32     if (areacode[i] <= code && code < max)
    33     {
    34     return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
    35     }
    36 }
    37 return "*";
    38     }
    39     else return cnChar;
    40 }

    控制台运行结果为:

  • 相关阅读:
    js实现小球碰撞游戏
    jquery实现简易的计算器
    js中的substr和substring区别
    学习笔记:模拟退火
    解题报告:luogu P4879
    解题报告:CF58C
    解题报告:luogu P1160
    解题报告:CF1244D
    学习笔记:三分法(唔,好像不是唉)
    解题报告: CF1288C
  • 原文地址:https://www.cnblogs.com/zsmj001/p/4092099.html
Copyright © 2011-2022 走看看