Home > Article > Web Front-end > GBK, UTF8 string actual length calculation function implemented by JavaScript_Basic knowledge
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: