JAVA验证字符串长度(包括汉字)

来源:互联网 发布:汉邦网络摄像机默认ip 编辑:程序博客网 时间:2024/06/05 09:40
/** 
     * 功能:验证字符串长度是否符合要求,一个汉字等于两个字符
     * @param strParameter 要验证的字符串
     * @param limitLength 验证的长度
     * @return 符合长度ture 超出范围false
     */
public boolean validateStrByLength(String strParameter , int limitLength)
{
     int temp_int=0;
     byte[] b=strParameter.getBytes();
  
     for(int i=0 ; i<b.length ; i++)
     {
      if(b[i]>=0)
      {
       temp_int=temp_int+1;
      }
      else
      {
       temp_int=temp_int+2;
       i++;
      }
     }
  
     if(temp_int > limitLength)
     {
      return false;
     }
     else
     {
      return true;
     }
}
0 0