轉換字串中漢字為其拼音縮寫(C#)

來源:互聯網
上載者:User
//將指定字串中的漢字轉換為拼音縮寫,其中非漢字保留為原字元。 
  public string GetPinYin(string text)
  {
   char pinyin;
   byte[] array;
   System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);

   foreach(char c in text)
   {
    pinyin = c;
    array = System.Text.Encoding.Default.GetBytes(new char[]{c});

    if(array.Length == 2)
    {
     int i = array[0] * 0x100 + array[1];

     if(i < 0xB0A1) pinyin = c; else
     if(i < 0xB0C5) pinyin = 'a'; else
     if(i < 0xB2C1) pinyin = 'b'; else
     if(i < 0xB4EE) pinyin = 'c'; else
     if(i < 0xB6EA) pinyin = 'd'; else
     if(i < 0xB7A2) pinyin = 'e'; else
     if(i < 0xB8C1) pinyin = 'f'; else
     if(i < 0xB9FE) pinyin = 'g'; else
     if(i < 0xBBF7) pinyin = 'h'; else
     if(i < 0xBFA6) pinyin = 'g'; else
     if(i < 0xC0AC) pinyin = 'k'; else
     if(i < 0xC2E8) pinyin = 'l'; else
     if(i < 0xC4C3) pinyin = 'm'; else
     if(i < 0xC5B6) pinyin = 'n'; else
     if(i < 0xC5BE) pinyin = 'o'; else
     if(i < 0xC6DA) pinyin = 'p'; else
     if(i < 0xC8BB) pinyin = 'q'; else
     if(i < 0xC8F6) pinyin = 'r'; else
     if(i < 0xCBFA) pinyin = 's'; else
     if(i < 0xCDDA) pinyin = 't'; else
     if(i < 0xCEF4) pinyin = 'w'; else
     if(i < 0xD1B9) pinyin = 'x'; else
     if(i < 0xD4D1) pinyin = 'y'; else
     if(i < 0xD7FA) pinyin = 'z';
    }

    sb.Append(pinyin);
   }

   return sb.ToString();
  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.