Method One:
var jmz = {};
Jmz. GetLength = function (str) {
///<summary> gets the actual length of the string, Chinese 2, English 1</summary>
///<param name= "str" > to get the length of the string </param>
var reallength = 0, Len = str.length, charcode =-1;
for (var i = 0; i < len; i++) {
charcode = str.charcodeat (i);
if (charcode >= 0 && charcode <= 128)
reallength + = 1;
else
Reallength + + 2;
}
return reallength;
};
Alert (JMZ. GetLength (' Test test Ceshiceshi ');
Method Two (more concise method):
var L = str.length;
var blen = 0;
For (i=0 i<l; i++) {
if (str.charcodeat (i) & 0xff00)!= 0) {
Blen + +;
}
Blen + +;
}
Method Three (more concise method):
var jmz = {};
Jmz. GetLength = function (str) {return
str.replace (/[\u0391-\uffe5]/g, "AA"). Length; First replace Chinese with two bytes in English, in the calculation of length
};
Alert (JMZ. GetLength (' Test test Ceshiceshi '));
This JS to get the actual length of the string (including Chinese characters) is a simple way to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.