• 【转贴】 C#中判断字符串是全角还是半角


    全角是指用二个字节来表示的一个字符
    半角是用一个字节来表示的一个字符
     
    这样的话我们就可以用string.length 和System.text.Encoding.Default.GetByteCount来判断
     
    其中string.length表示字符串的字符数,
    System.text.Encoding.Default.GetByteCount表示字符串的字节数。
     
    将全角的变为半角  
      string   s="GBJ1—86";  
      char[]   c=s.ToCharArray();  
      for   (int   i=0;i<c.Length;i++)  
      {  
          byte[]   b=System.Text.Encoding.Unicode.GetBytes(c,i,1);  
          if   (b.Length==2)  
          {  
              if   (b[1]==255)  
              {  
                  b[0]=(byte)(b[0]+32);  
                  b[1]=0;  
                  c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];  
              }  
          }  
      }  
      //半角  
      string   news=new   string(c); 
     
    把字母,数字由半角转化为全角  
      public   string     ChangeStr(   string   str)  
      {  
          char[]   c=str.ToCharArray();  
          for   (int   i=0;i<c.Length;i++)  
          {  
          byte[]   b=System.Text.Encoding.Unicode.GetBytes(c,i,1);  
          if   (b.Length==2)  
          {  
                  if   (b[1]==0)  
                  {  
                          b[0]=(byte)(b[0]-32);  
                          b[1]=255;  
                          c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];  
                  }  
          }  
      }  
      //半角  
      string   strNew=new   string(c);  
      return   strNew;  
      }  
     
    判断的方法一:  
    代码测试报告:只能对单个字符进行判断,如果出现"23"    判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
      string   s   =   null;  
       
      s   =   "A";  
      MessageBox.Show(((s[0]   >   255)   ?   "全角"   :   "半角")   +   "   ASCII   of   "   +   Convert.ToInt32(s[0]).ToString("x").ToUpper());  
       
      s   =   "A";  
      MessageBox.Show(((s[0]   >   255)   ?   "全角"   :   "半角")   +   "   ASCII   of   "   +   Convert.ToInt32(s[0]).ToString("x").ToUpper());  
       
      //中文的Unicode大概是从4E00   到   9FA0,所以上例一个是0x41   一个是0xFF21
     
    判断的方法二:
    代码测试报告:只能对单个字符进行判断,如果出现"23"    判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
    if (checkString.Length == Encoding.Default.GetByteCount(checkString))
       {
        return true;
       }
       else
       {
        return false;
       }
     
    全角如下:
    if (2 * checkString.Length ==  Encoding.Default.GetByteCount(checkString))
       {
        return true;
       }
       else
       {
        return false;
       }


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/foart/archive/2008/10/04/3014019.aspx

    Example:

    判断一个字符是全角还是半角(占一个字节还是两个字节

    String   source   =   "ls;;'立刻地方机十分kd   立刻地方"; 
              int   length   =   source.length(); 
              for(int   i=0;i<length;i++) 
              { 
                  char   c   =   source.charAt(i); 
                  if   (c   >   255) 
                  { 
                      System.out.print("中文   "); 
                  } 
                  System.out.println(c); 
              } 

  • 相关阅读:
    JavaSript模块化 && AMD CMD 详解.....
    js实现touch移动触屏滑动事件
    页面布局之BFC 微微有点坑
    前端代码优化
    HTTP消息头详解
    SASS
    移动互联,手机页面设计
    投身移动开发必须知道的20件事
    浅析HTML5在移动应用开发中的使用
    js数组的操作
  • 原文地址:https://www.cnblogs.com/daytoday/p/1786723.html
Copyright © 2020-2023  润新知