zoukankan      html  css  js  c++  java
  • 字符串转换(全角/半角)

     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 
  • 相关阅读:
    《WCF全面解析》-上册 1-3章 读书笔记
    git上传本地Intellij idea 项目到码云的git仓库中
    Node.js安装及环境配置之Windows篇
    让IntelliJ IDEA支持创建*.vue文件及打开*.vue文件
    maven命令mvn package指定jar包名称
    spring 时间格式化注解@DateTimeFormat和 @JsonFormat
    事务及事务隔离级别
    @RequestBody的使用
    接口jdk1.8与jdk1.9新特性
    Spring AOP无法拦截内部方法调用-- expose-proxy="true"用法
  • 原文地址:https://www.cnblogs.com/tearer/p/1778256.html
Copyright © 2011-2022 走看看