中文字符长度判断

中文字符长度判断
javascript 中文字符长度判断

if (srt.charCodeAt(i)  >   255 {
//if(friendnick.charCodeAt(i) < 0x4E00 || friendnick.charCodeAt(i) > 0x9FA5) {
     charlen 
+=2;
    }
else {
    
     charlen 
+=1;
    }

java 中文字符长度判断
  if (tempProperty.getBytes().length  >  cut * 2  ) {
                
if (tempProperty.length() >= (cut + 1)) {
                    
byte [] src = tempProperty.getBytes();
                    
byte [] dest = new byte[cut*2+2];
                    
int flag = 0;
                    
for(int j=0;j<src.length;j++){  
                        
if(j<= cut*2){
                           
// System.out.println(j);
                            if(src[j]<0)
                                flag
++;
                            dest[j]
= src[j];
                        }

                    }

                  
                    
if(flag % 2 !=0){
                        dest[cut
*2]= 0;
                    }

                    tempProperty 
=  new String(dest).trim()+ ""
这个代码有问题  "sss顺磾村厈士大夫"  tempProperty.getBytes() 其中一个数字不为负数
  修改过的代码

 String s  =   " stss顺磾村厈士大夫 " ;
        String badChar 
= " ABCDEFGHIJKLMNOPQRSTUVWXYZ "
        badChar 
+=   " abcdefghijklmnopqrstuvwxyz "
        badChar 
+=   " 0123456789 "
        badChar 
+=   "   " + "   " ; // 半角与全角空格 
        badChar  +=   " .`~!@#$%^&()-_=+]\\|:;\ " \ ' <,>?/*";
        String result  =   "" ;
        
int  len  =   0 ;
            
for ( int  i = 0 ;i < s.length();i ++ )
                
char c = s.charAt(i);
                
if(badChar.indexOf(c)==-1){//如果是中文
                   result = result+c;
                   len 
+= 2;
                }
else{
                   result 
=  result+c;
                   len 
+= 1;
                }

                
if(len>=15break;
            }
 

你可能感兴趣的:(中文字符长度判断)