Home  >  Article  >  Web Front-end  >  How to get the string length in javascript

How to get the string length in javascript

青灯夜游
青灯夜游Original
2021-06-29 16:56:3542945browse

Javascript method to get the length of a string: 1. Use the length attribute to get the length of the string by characters, the syntax is "string.length"; 2. Use charCodeAt() to get the length of the string by bytes.

How to get the string length in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

js Determine the length of a string by characters

The length attribute can read the length of the string. The length is in characters and this property is read-only.
Sample code:

<script>
    var str = &#39;hello word!&#39;;
    console.log(str.length);
    var str2 = &#39;你好,世界!你好,中国!&#39;;
    console.log(str2.length);
</script>

Output result:

11 12

Note:

1. Use the length attribute to get the length. Each character, including spaces and punctuation marks, is counted. One character

2. In the length attribute, Chinese characters also default to one character

js determines the length of a string by bytes

The bytes supported in JavaScript include single-byte and double-byte types. Generally speaking, English and English symbols occupy 1 character, and Chinese characters and Chinese symbols occupy 2 characters.

Example 1:

charCodeAt(): The method returns the Unicode encoding of the character at the specified position. Its value is an integer between 0 - 65535.

<script>
    var str = &#39;电影:我和我的祖国&#39;;
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
            len += 2;
        } else {
            len++;
        }
    }
    console.log(len);
</script>

Output result:

18

Example 2:

A Chinese character is 2 characters, and a Chinese symbol is two characters.

<script>
    var str = &#39;你好,世界!&#39;;
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1   
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
            len++;
        } else {
            len += 2;
        }
    }   
    console.log(len);
</script>

Output result: 12

Example 3:

Replace the double-byte character with two single-byte characters and get its characters Number

<script>
    var str = &#39;飞鸟慕鱼博客&#39;;
    var len = str.replace(/[^\x00-\xff]/g, "00").length; 
    console.log(len);
</script>

Run result: 12

Example 4:

<script>
    var str = &#39;中华人民共和国&#39;;
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        var c = str.charAt(i);
        if (escape(c).length > 4) {
            len += 2;
        } else if (c != "\r") { 
            len++; 
        }
    }   
    console.log(len);
</script>

Run result: 14

Example 5:

You can use regular expressions to determine whether a character is double bytes

<script>
    var str = &#39;这里使用的正规表达式&#39;;
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        var a = str.charAt(i);
        //使用的正则表达式
        if (a.match(/[^\x00-\xff]/ig) != null) {
            len += 2;
        } else {
            len += 1;
        }
    }   
    console.log(len);
</script>
//20

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to get the string length in javascript. For more information, please follow other related articles on the PHP Chinese website!

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