有什么好的改进,希望大家共同探讨:原文链接
1 1using System;
2 2using System.Data;
3 3using System.Configuration;
4 4using System.Collections;
5 5using System.Web;
6 6using System.Web.Security;
7 7using System.Web.UI;
8 8using System.Web.UI.WebControls;
9 9using System.Web.UI.WebControls.WebParts;
10 10using System.Web.UI.HtmlControls;
11 11
12 12public partial class 汉字转拼音函数_Default : System.Web.UI.Page
13 13{
14 14 protected void Page_Load(object sender, EventArgs e)
15 15 {
16 16 this.lbl_Show.Text = GetPYString("郑玉路");
17 17 }
18 18
19 19 /**//// <summary>
20 20 /// 汉字转拼音缩写
21 21 /// Code By MuseStudio@hotmail.com
22 22 /// 2004-11-30
23 23 /// </summary>
24 24 /// <param name="str">要转换的汉字字符串</param>
25 25 /// <returns>拼音缩写</returns>
26 26 public string GetPYString(string str)
27 27 {
28 28 string tempStr = "";
29 29 foreach (char c in str)
30 30 {
31 31 if ((int)c >= 33 && (int)c <= 126)
32 32 {//字母和符号原样保留
33 33 tempStr += c.ToString();
34 34 }
35 35 else
36 36 {//累加拼音声母
37 37 tempStr += GetPYChar(c.ToString());
38 38 }
39 39 }
40 40 return tempStr;
41 41 }
42 42
43 43 /**//// <summary>
44 44 /// 取单个字符的拼音声母
45 45 /// Code By MuseStudio@hotmail.com
46 46 /// 2004-11-30
47 47 /// </summary>
48 48 /// <param name="c">要转换的单个汉字</param>
49 49 /// <returns>拼音声母</returns>
50 50 public string GetPYChar(string c)
51 51 {
52 52 byte[] array = new byte[2];
53 53 array = System.Text.Encoding.Default.GetBytes(c);
54 54 int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
55 55
56 56 if (i < 0xB0A1) return "*";
57 57 if (i < 0xB0C5) return "a";
58 58 if (i < 0xB2C1) return "b";
59 59 if (i < 0xB4EE) return "c";
60 60 if (i < 0xB6EA) return "d";
61 61 if (i < 0xB7A2) return "e";
62 62 if (i < 0xB8C1) return "f";
63 63 if (i < 0xB9FE) return "g";
64 64 if (i < 0xBBF7) return "h";
65 65 if (i < 0xBFA6) return "g";
66 66 if (i < 0xC0AC) return "k";
67 67 if (i < 0xC2E8) return "l";
68 68 if (i < 0xC4C3) return "m";
69 69 if (i < 0xC5B6) return "n";
70 70 if (i < 0xC5BE) return "o";
71 71 if (i < 0xC6DA) return "p";
72 72 if (i < 0xC8BB) return "q";
73 73 if (i < 0xC8F6) return "r";
74 74 if (i < 0xCBFA) return "s";
75 75 if (i < 0xCDDA) return "t";
76 76 if (i < 0xCEF4) return "w";
77 77 if (i < 0xD1B9) return "x";
78 78 if (i < 0xD4D1) return "y";
79 79 if (i < 0xD7FA) return "z";
80 80
81 81 return "*";
82 82 }
83 83
84 84}
85 85