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

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

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

测试代码如下:
1 protected void Button1_Click(object sender, EventArgs e)
 protected void Button1_Click(object sender, EventArgs e)
2 {
        {
3 this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);
            this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);
4 this.Label2.Text = "提取第一个字母:" + Nimeux.Common.WordTransform.Transform(this.TextBox1.Text);
            this.Label2.Text = "提取第一个字母:" + Nimeux.Common.WordTransform.Transform(this.TextBox1.Text);
5 }
        }
 protected void Button1_Click(object sender, EventArgs e)
 protected void Button1_Click(object sender, EventArgs e)2
 {
        {3
 this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);
            this.Label1.Text = "汉字转成全拼:" + Nimeux.Common.WordTransform.ConvertE(this.TextBox1.Text);4
 this.Label2.Text = "提取第一个字母:" + Nimeux.Common.WordTransform.Transform(this.TextBox1.Text);
            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 />
<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="Label1">汉字转成全拼:NiHao</span> <br /> <span id="Label2">提取第一个字母:n</span> </p>
<span id="Label2">提取第一个字母:n</span> </p> 
                    
                 


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号