posts - 120,  comments - 19,  trackbacks - 0
//对于gb2312来讲, 首字节码位从0×81 至0×FE,尾字节码位分别是0×40 至0×FE
//本例是验证此串是否含有gb2312格式的字符,即是否含有汉字
public class Test{
  public boolean isGB2312( String str )
  {
    char[] chars = str.toCharArray();
    boolean isGB2312 = false;
    for ( int i = 0; i < chars.length; i++ )
    {
      byte[] bytes = ( "" + chars[i] ).getBytes();
      if ( bytes.length == 2 )
      {
        int[] ints = new int[2];
        ints[0] = bytes[0] & 0xff;
        ints[1] = bytes[1] & 0xff;
        if ( ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE )
        {
          isGB2312 = true;
          break;
        }
      }
    }
    return isGB2312;
  }
 
  public static void main(String[] args)
  {
    String s = "ss您好ss";//结果为true
    String s = "ssssss";//结果为false
    Test test = new Test();
    System.out.println(test.isGB2312(s));
  }
}

posted on 2006-08-16 16:38 阿成 阅读(454) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: