# HOWTO: 判断全角和半角

ASCII码大于255，则为双字节？

1,FCL

System.Text.UnicodeEncoding.Unicode.GetByteCoun(str)

2,Regex

3,第三方实现

   1:  #region 半全角互换
   2:  /// <summary>
   3:  /// 转全角的函数(SBC case)
   4:  /// </summary>
   5:  /// <param name="input">任意字符串</param>
   6:  /// <returns>全角字符串</returns>
   7:  ///<remarks>
   8:  ///全角空格为12288，半角空格为32

9: ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248

  10:  ///</remarks>
  11:  public static string ToSBC(string input)
  12:  {
  13:      //半角转全角：
  14:      char[] c = input.ToCharArray();
  15:      for (int i = 0; i < c.Length; i++)
  16:      {
  17:          if (c[i] == 32)
  18:          {
  19:              c[i] = (char)12288;
  20:              continue;
  21:          }
  22:          if (c[i] < 127 && c[i] > 32)
  23:          {
  24:              c[i] = (char)(c[i] + 65248);
  25:          }
  26:      }
  27:      return new string(c);
  28:  }
  29:
  30:  /// <summary>
  31:  /// 转半角的函数(DBC case)
  32:  /// </summary>
  33:  /// <param name="input">任意字符串</param>
  34:  /// <returns>半角字符串</returns>
  35:  ///<remarks>
  36:  ///全角空格为12288，半角空格为32(此处不必转空格)

37: ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248

  38:  ///</remarks>
  39:  public static string ToDBC(string input)
  40:  {
  41:      char[] c = input.ToCharArray();
  42:      for (int i = 0; i < c.Length; i++)
  43:      {
  44:          if (c[i] == 12288)
  45:          {
  46:              c[i] = (char)32;
  47:              continue;
  48:          }
  49:          if (c[i] > 65280 && c[i] < 65375)
  50:          {
  51:              c[i] = (char)(c[i] - 65248);
  52:          }
  53:      }
  54:      return new string(c);
  55:  }
  56:  #endregion
posted @ 2006-12-03 18:32  sinlight23  阅读(743)  评论(0编辑  收藏  举报