字符串转换(全角/半角)

 1 /// <summary>
 2   /// 字符串转换(全角/半角)
 3   /// </summary>
 4   public class BCCase
 5   {
 6     private BCCase() { }
 7 
 8     private static BCCase _instance = new BCCase();
 9 
10     public static BCCase _
11     {
12       get { return _instance; }
13       set { _instance = value; }
14     }
15 
16     #region 全角转换
17     /// <summary>
18     /// 全角转换
19     /// </summary>
20     /// <param name="input">全角字符</param>
21     /// <returns>输出半角</returns>
22     public string GetSBC(string input)
23     {
24       char[] c = input.ToCharArray();
25       for (int i = 0; i < c.Length; i++)
26       {
27         byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
28         if (b.Length == 2)
29         {
30           if (b[1== 0)
31           {
32             b[0= (byte)(b[0- 32);
33             b[1= 255;
34             c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
35           }
36         }
37       }
38       string strNew = new string(c);
39       return strNew;
40     }
41     #endregion
42 
43     #region 半角转换
44     /// <summary>
45     /// 半角转换
46     /// </summary>
47     /// <param name="input">半角字符</param>
48     /// <returns>输出全角</returns>
49     public string GetDBC(string input)
50     {
51       char[] c = input.ToCharArray();
52       for (int i = 0; i < c.Length; i++)
53       {
54         byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
55         if (b.Length == 2)
56         {
57           if (b[1== 255)
58           {
59             b[0= (byte)(b[0+ 32);
60             b[1= 0;
61             c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
62           }
63         }
64       }
65       string strNew = new string(c);
66       return strNew;
67     }
68     #endregion
69   }
70 
原文地址:https://www.cnblogs.com/tearer/p/1778256.html