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

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

May 16, 2016 pm 04:38 PM
gbk javascript utf8

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

Go language encoding analysis: UTF-8 and GBK comparison Go language encoding analysis: UTF-8 and GBK comparison Mar 28, 2024 pm 01:54 PM

Go language encoding analysis: UTF-8 and GBK comparison

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

See all articles