freeCodeCamp/guide/chinese/javascript/standard-objects/string/string-length/index.md

1.4 KiB
Raw Blame History

title localeTitle
String Length 字符串长度

length属性表示字符串的长度。

句法

str.length 

MDN链接 | MSDN链接

描述

此属性返回字符串中的代码单元数。 UTF-16是JavaScript使用的字符串格式使用单个16位代码单元来表示最常见的字符但需要使用两个代码单元来表示不常用的字符因此有可能将长度返回的值返回到不匹配字符串中的实际字符数。

对于空字符串长度为0。

静态属性String.length返回值1。

例子

var x = 'Mozilla'; 
 var empty = ''; 
 
 console.log('Mozilla is ' + x.length + ' code units long'); 
 /* "Mozilla is 7 code units long" */ 
 
 console.log('The empty string has a length of ' + empty.length); 
 /* "The empty string has a length of 0" */ 
 
 var str = "every good boy does fine"; 
        var start = 0; 
        var end = str.length - 1; 
        var tmp = ""; 
        var arr = new Array(end); 
 
        while (end >= 0) { 
            arr[start++] = str.charAt(end--); 
        } 
 
 // Join the elements of the array with a 
        var str2 = arr.join(''); 
        document.write(str2); 
 
 // Output: enif seod yob doog yreve