汉字截取类
现在决定在博客园安家了,所以把CSDN上以前写的一些代码和相关的感想搬到这里。这个类是在一个项目中要用的一个功能,就是把活跃的100个博主的博客名称按照英文A-Z排列。因为不用用户注册的博客名有汉字有英文,所以写了这么一个类。该功能类的代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Text.RegularExpressions;
5
6
namespace Nimeux.Common
7
{
8
public class WordTransform
9
{
10
public WordTransform()
11
{ }
12
/// <summary>
13
/// 输入一个中文字,返回拼音的第一个小写字母
14
/// </summary>
15
/// <param name="cn">输入的中文字</param>
16
/// <returns></returns>
17
static public string WordSpell(string cn)
18
{
19
byte[] arrCN = Encoding.Default.GetBytes(cn);
20
if (arrCN.Length > 1)
21
{
22
int firstWord = (short)arrCN[0];
23
int secondWord = (short)arrCN[1];
24
int code = (firstWord << 8) + secondWord;
25
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 };
26
for (int i = 0; i < 26; i++)
27
{
28
int max = 55290;
29
if (i != 25)
30
max = areacode[i + 1];
31
if (areacode[i] <= code && code < max)
32
{
33
//获取大写字母
34
return Encoding.Default.GetString(new byte[] { (byte)(97 + i) });
35
}
36
}
37
return "*";
38
}
39
else
40
return cn;
41
}
42
/// <summary>
43
/// 输入一组中文,输出每个中文字拼音的小写
44
/// </summary>
45
/// <param name="cns">一组中文</param>
46
/// <returns></returns>
47
static public string WordsSpell(string cns)
48
{
49
int num = cns.Length;
50
string result = "";
51
for (int i = 0; i < num; i++)
52
{
53
result += WordSpell(cns.Substring(i, 1));
54
}
55
return result;
56
}
57
/// <summary>
58
/// 判断输入的字符串是否是汉字
59
/// </summary>
60
/// <param name="str">输入的字符串</param>
61
/// <returns></returns>
62
static public bool MatchCN(string str)
63
{
64
string flag = @"[\u4e00-\u9fa5]";
65
Regex rg = new Regex(flag);
66
if (rg.IsMatch(str))
67
{
68
return true;
69
}
70
else
71
{
72
return false;
73
}
74
}
75
/// <summary>
76
/// 提取输入字符串的第一个字母,如果是中文则提取拼音的第一个字母
77
/// </summary>
78
/// <param name="input">输入字符串</param>
79
/// <returns></returns>
80
static public string Transform(string input)
81
{
82
string temp = string.Format(input);
83
if (MatchCN(temp))
84
{
85
return WordSpell(temp);
86
}
87
else
88
{
89
string result = "";
90
for (int i = 0; i < 26; i++)
91
{
92
if (temp.Substring(0, 1) == Encoding.Default.GetString(new byte[] { (byte)(65 + i) }))
93
{
94
result = Encoding.Default.GetString(new byte[] { (byte)(97 + i) });
95
break;
96
}
97
else
98
{
99
result = temp.Substring(0, 1);
100
}
101
}
102
return result;
103
}
104
}
105
106
把汉字转化成全拼音
242
}
243
}
244
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Text.RegularExpressions;5

6
namespace Nimeux.Common7
{8
public class WordTransform9
{10
public WordTransform()11
{ }12
/// <summary>13
/// 输入一个中文字,返回拼音的第一个小写字母14
/// </summary>15
/// <param name="cn">输入的中文字</param>16
/// <returns></returns>17
static public string WordSpell(string cn)18
{19
byte[] arrCN = Encoding.Default.GetBytes(cn);20
if (arrCN.Length > 1)21
{22
int firstWord = (short)arrCN[0];23
int secondWord = (short)arrCN[1];24
int code = (firstWord << 8) + secondWord;25
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 };26
for (int i = 0; i < 26; i++)27
{28
int max = 55290;29
if (i != 25)30
max = areacode[i + 1];31
if (areacode[i] <= code && code < max)32
{33
//获取大写字母34
return Encoding.Default.GetString(new byte[] { (byte)(97 + i) });35
}36
}37
return "*";38
}39
else40
return cn;41
}42
/// <summary>43
/// 输入一组中文,输出每个中文字拼音的小写44
/// </summary>45
/// <param name="cns">一组中文</param>46
/// <returns></returns>47
static public string WordsSpell(string cns)48
{49
int num = cns.Length;50
string result = "";51
for (int i = 0; i < num; i++)52
{53
result += WordSpell(cns.Substring(i, 1));54
}55
return result;56
}57
/// <summary>58
/// 判断输入的字符串是否是汉字59
/// </summary>60
/// <param name="str">输入的字符串</param>61
/// <returns></returns>62
static public bool MatchCN(string str)63
{64
string flag = @"[\u4e00-\u9fa5]";65
Regex rg = new Regex(flag);66
if (rg.IsMatch(str))67
{68
return true;69
}70
else71
{72
return false;73
}74
}75
/// <summary>76
/// 提取输入字符串的第一个字母,如果是中文则提取拼音的第一个字母77
/// </summary>78
/// <param name="input">输入字符串</param>79
/// <returns></returns>80
static public string Transform(string input)81
{82
string temp = string.Format(input);83
if (MatchCN(temp))84
{85
return WordSpell(temp);86
}87
else88
{89
string result = "";90
for (int i = 0; i < 26; i++)91
{92
if (temp.Substring(0, 1) == Encoding.Default.GetString(new byte[] { (byte)(65 + i) }))93
{94
result = Encoding.Default.GetString(new byte[] { (byte)(97 + i) });95
break;96
}97
else98
{99
result = temp.Substring(0, 1);100
}101
}102
return result;103
}104
}105

106
把汉字转化成全拼音242
}243
}244

测试代码如下:
1
protected void Button1_Click(object sender, EventArgs e)
2
{
3
this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);
4
this.Label2.Text = "提取第一个字母:" + Nimeux.Common.WordTransform.Transform(this.TextBox1.Text);
5
}
protected void Button1_Click(object sender, EventArgs e)2
{3
this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);4
this.Label2.Text = "提取第一个字母:" + Nimeux.Common.WordTransform.Transform(this.TextBox1.Text);5
}
这显示如下:
<p><input id="TextBox1" value="你好" name="TextBox1" type="text" /> <input id="Button1" type="submit" value="Button" name="Button1" /> <br />
<span id="Label1">汉字转成全拼:NiHao</span> <br />
<span id="Label2">提取第一个字母:n</span> </p>


浙公网安备 33010602011771号