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>=15) break;
}

//if(friendnick.charCodeAt(i) < 0x4E00 || friendnick.charCodeAt(i) > 0x9FA5) {





java 中文字符長度判斷



















修改過的代碼


















