Home  >  Article  >  Web Front-end  >  GBK, UTF8 string actual length calculation function implemented by JavaScript_Basic knowledge

GBK, UTF8 string actual length calculation function implemented by JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:38:332036browse

As we all know, the length of strings in JS does not distinguish between Chinese and English characters. Each character counts as a length, which is different from the strlen() function in PHP. The strlen() function in PHP accumulates GBK Chinese characters by 2 and UTF-8 Chinese characters by 3 according to the character set.

Some children’s shoes may ask, why do we need to calculate the actual length?

Mainly to match the length range of the database. For example, a field in GBK's database is varchar(10), which is equivalent to the length of 5 Chinese characters, and one Chinese character is equal to the length of two letters. If it is a UTF8 database, the length of each Chinese character is 3.

After knowing the above principles, we can calculate the actual length of a string. If it is a GBK character set, add 2 when encountering Chinese characters. If it is a UTF8 character set, add 3 when encountering Chinese characters.
GBK length calculation function:

Copy code The code is as follows:

// Actual length calculation 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 is Chinese, add 2 to the length
               realLength = 2;
        }
}  
Return realLength;
}

UTF8 length calculation function:
Copy code The code is as follows:

// UTF8 character set actual length calculation
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 is Chinese, add 3 to the length
               realLength = 3;
        }
}  
Return realLength;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn