字符串:length
String
值的 length
数据属性包含 UTF-16 代码单元中的字符串长度。
¥The length
data property of a String
value contains the length of the string in UTF-16 code units.
Try it
值
描述
¥Description
此属性返回字符串中的代码单元数。JavaScript 使用 UTF-16 编码,其中每个 Unicode 字符可能被编码为一个或两个代码单元,因此 length
返回的值可能与字符串中的实际 Unicode 字符数不匹配。对于拉丁文、西里尔文、众所周知的 CJK 字符等常见脚本,这应该不是问题,但如果你正在使用某些脚本,例如表情符号、数学符号 或晦涩的中文字符,则可能需要考虑差异 代码单元和字符之间。
¥This property returns the number of code units in the string. JavaScript uses UTF-16 encoding, where each Unicode character may be encoded as one or two code units, so it's possible for the value returned by length
to not match the actual number of Unicode characters in the string. For common scripts like Latin, Cyrillic, wellknown CJK characters, etc., this should not be an issue, but if you are working with certain scripts, such as emojis, mathematical symbols, or obscure Chinese characters, you may need to account for the difference between code units and characters.
语言规范要求字符串的最大长度为 253 - 1 个元素,这是 精确整数 的上限。然而,这个长度的字符串需要 16384TiB 的存储空间,这无法适应任何合理的设备内存,因此实现往往会降低阈值,这使得字符串的长度可以方便地存储在 32 位整数中。
¥The language specification requires strings to have a maximum length of 253 - 1 elements, which is the upper limit for precise integers. However, a string with this length needs 16384TiB of storage, which cannot fit in any reasonable device's memory, so implementations tend to lower the threshold, which allows the string's length to be conveniently stored in a 32-bit integer.
- 在 V8(Chrome 和 Node 使用)中,最大长度为 229 - 24(~1GiB)。在 32 位系统上,最大长度为 228 - 16(~512MiB)。
- 在 Firefox 中,最大长度为 230 - 2(~2GiB)。在 Firefox 65 之前,最大长度为 228 - 1 (~512MiB)。
- 在 Safari 中,最大长度为 231 - 1(~4GiB)。
如果你正在使用其他编码(例如 UTF-8 文件或 blob)的大字符串,请注意,当你将数据加载到 JS 字符串中时,编码始终会变为 UTF-16。字符串的大小可能与源文件的大小不同。
¥If you are working with large strings in other encodings (such as UTF-8 files or blobs), note that when you load the data into a JS string, the encoding always becomes UTF-16. The size of the string may be different from the size of the source file.
const str1 = "a".repeat(2 ** 29 - 24); // Success
const str2 = "a".repeat(2 ** 29 - 23); // RangeError: Invalid string length
const buffer = new Uint8Array(2 ** 29 - 24).fill("a".codePointAt(0)); // This buffer is 512MiB in size
const str = new TextDecoder().decode(buffer); // This string is 1GiB in size
对于空字符串,length
为 0。
¥For an empty string, length
is 0.
静态属性 String.length
与字符串的长度无关。它是 String
函数的 arity(宽松地说,它具有的形式参数的数量),即 1。
¥The static property String.length
is unrelated to the length of strings. It's the arity of the String
function (loosely, the number of formal parameters it has), which is 1.
由于 length
计算的是代码单元而不是字符,因此如果要获取字符数,可以先将字符串与其 iterator 分割,后者按字符进行迭代:
¥Since length
counts code units instead of characters, if you want to get the number of characters, you can first split the string with its iterator, which iterates by characters:
function getCharacterLength(str) {
// The string iterator that is used here iterates over characters,
// not mere code units
return [...str].length;
}
console.log(getCharacterLength("A\uD87E\uDC04Z")); // 3
如果要按字素簇计算字符数,请使用 Intl.Segmenter
。你可以先将要拆分的字符串传递给 segment()
方法,然后迭代返回的 Segments
对象以获取长度:
¥If you want to count characters by grapheme clusters, use Intl.Segmenter
. You can first pass the string you want to split to the segment()
method, and then iterate over the returned Segments
object to get the length:
function getGraphemeCount(str) {
const segmenter = new Intl.Segmenter("en-US", { granularity: "grapheme" });
// The Segments object iterator that is used here iterates over characters in grapheme clusters,
// which may consist of multiple Unicode characters
return [...segmenter.segment(str)].length;
}
console.log(getGraphemeCount("👨👩👧👧")); // 1
示例
基本用法
长度不等于字符数的字符串
¥Strings with length not equal to the number of characters
const emoji = "😄";
console.log(emoji.length); // 2
console.log([...emoji].length); // 1
const adlam = "𞤲𞥋𞤣𞤫";
console.log(adlam.length); // 8
console.log([...adlam].length); // 4
const formula = "∀𝑥∈ℝ,𝑥²≥0";
console.log(formula.length); // 11
console.log([...formula].length); // 9
分配给长度
¥Assigning to length
由于字符串是基元,因此尝试为字符串的 length
属性赋值不会产生明显的效果,并且会抛出 严格模式 异常。
¥Because string is a primitive, attempting to assign a value to a string's length
property has no observable effect, and will throw in strict mode.
const myString = "bluebells";
myString.length = 4;
console.log(myString); // "bluebells"
console.log(myString.length); // 9
规范
Specification |
---|
ECMAScript Language Specification # sec-properties-of-string-instances-length |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also