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

12
public partial class 汉字转拼音函数_Default : System.Web.UI.Page13
{14
protected void Page_Load(object sender, EventArgs e)15
{16
this.lbl_Show.Text = GetPYString("郑玉路");17
}18

19
/// <summary>20
/// 汉字转拼音缩写21
/// Code By MuseStudio@hotmail.com22
/// 2004-11-3023
/// </summary>24
/// <param name="str">要转换的汉字字符串</param>25
/// <returns>拼音缩写</returns>26
public string GetPYString(string str)27
{28
string tempStr = "";29
foreach (char c in str)30
{31
if ((int)c >= 33 && (int)c <= 126)32
{//字母和符号原样保留33
tempStr += c.ToString();34
}35
else36
{//累加拼音声母37
tempStr += GetPYChar(c.ToString());38
}39
}40
return tempStr;41
}42

43
/// <summary>44
/// 取单个字符的拼音声母45
/// Code By MuseStudio@hotmail.com46
/// 2004-11-3047
/// </summary>48
/// <param name="c">要转换的单个汉字</param>49
/// <returns>拼音声母</returns>50
public string GetPYChar(string c)51
{52
byte[] array = new byte[2];53
array = System.Text.Encoding.Default.GetBytes(c);54
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));55

56
if (i < 0xB0A1) return "*";57
if (i < 0xB0C5) return "a";58
if (i < 0xB2C1) return "b";59
if (i < 0xB4EE) return "c";60
if (i < 0xB6EA) return "d";61
if (i < 0xB7A2) return "e";62
if (i < 0xB8C1) return "f";63
if (i < 0xB9FE) return "g";64
if (i < 0xBBF7) return "h";65
if (i < 0xBFA6) return "g";66
if (i < 0xC0AC) return "k";67
if (i < 0xC2E8) return "l";68
if (i < 0xC4C3) return "m";69
if (i < 0xC5B6) return "n";70
if (i < 0xC5BE) return "o";71
if (i < 0xC6DA) return "p";72
if (i < 0xC8BB) return "q";73
if (i < 0xC8F6) return "r";74
if (i < 0xCBFA) return "s";75
if (i < 0xCDDA) return "t";76
if (i < 0xCEF4) return "w";77
if (i < 0xD1B9) return "x";78
if (i < 0xD4D1) return "y";79
if (i < 0xD7FA) return "z";80

81
return "*";82
}83

84
}85




浙公网安备 33010602011771号