Mainly to match the length of the database, such as the GBK database a field is varchar (10), then the equivalent of 5 characters length, one Chinese character is equal to two letter length. If the database is UTF8, the length of each Chinese character is 3.
Knowing the above principles, we can calculate the actual length of a string, if the GBK character set encountered in Chinese plus 2, if the UTF8 character set encountered Chinese plus 3 can
GBK Length calculation function:
The code is as follows:
Calculation of actual length of GBK character set
function Getstrleng (str) {
var reallength = 0;
var len = str.length;
var charcode =-1;
for (var i = 0; i < len; i++) {
CharCode = Str.charcodeat (i);
if (charcode >= 0 && charcode <= 128) {
Reallength + 1;
}else{
If it's Chinese then length plus 2
Reallength + 2;
}
}
return reallength;
}
UTF8 Length calculation function:
The code is as follows:
Calculation of actual length of UTF8 character set
function Getstrleng (str) {
var reallength = 0;
var len = str.length;
var charcode =-1;
for (var i = 0; i < len; i++) {
CharCode = Str.charcodeat (i);
if (charcode >= 0 && charcode <= 128) {
Reallength + 1;
}else{
If it's Chinese then length plus 3
Reallength + 3;
}
}
return reallength;
}