发布于 2026-01-06 0 阅读
0

你可能没听说过的 JavaScript 字符串方法概述

你可能没听说过的 JavaScript 字符串方法,这也没关系

概述

章鱼

概述

字符串是 JavaScript 中用于表示文本的数据类型,可以包含字母、数字、符号、标点符号,甚至表情符号。它们由零个或多个(16 位值)字符组成,并用单引号'或双"引号括起来。

这并非什么高深莫测的学问!之前的介绍没什么特别之处,但温故知新总是好的。信不信由你,很多开发者对 JavaScript 的所有预定义方法都一无所知,我们大多数人只是觉得那些记忆犹新的知识“够用”。例如,几乎所有人都知道要获取字符串的第一个字符,比如 `get_char("char")`,str[0]这当然没问题。本文将介绍5 个你可能听说过或没听说过的JavaScript 方法,并附上示例。

字符串方法和属性

由于字符串是基本类型之一,例如“Mask Off”,它本身不能拥有属性或方法。幸运的是,JavaScript 为字符串定义了多种属性和方法。这些属性和方法可以通过点号表示法访问。

在 JavaScript 中,字符串是不可变的,可以像只读数组一样使用。所有字符串方法都会返回一个新值,而不会修改调用它们时所处理的字符串。


示例设置

在深入探讨细节之前,我们先来设置一个可以在所有示例中使用的代码块:

const content = "Forem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronics typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem IpsumL";

const { length }  = content;    // 500 characters
const firstChar   = 0;          // F
const lastChar    = length - 1; // L
const midContent  = length / 2; // e
const outOfRange  = length * 2; // ""
Enter fullscreen mode Exit fullscreen mode

charAt() 方法

charAt()方法返回指定索引处的字符,如果索引超出范围,则返回空字符串。如果未提供索引参数,则默认值为 0。

/**
 * @param  {number} ranges from 0 to the length of the string -1
 * @return {string}
 */
string.charAt(index)
Enter fullscreen mode Exit fullscreen mode

charAt() 示例

content.charAt() // "F"
content.charAt(firstChar)  // "F"
content.charAt(lastChar)   // "L"
content.charAt(midContent) // "e"
content.charAt(outOfRange) // ""
Enter fullscreen mode Exit fullscreen mode

startsWith() 方法

startsWith()方法判断一个字符串是否以指定字符串的字符开头。

/**
 * @param  {string} string to search for
 * @param  {number} optional - index, defaults to 0
 * @return {boolean}
 */
string.startsWith(string, position)
Enter fullscreen mode Exit fullscreen mode

startsWith()方法区分大小写

startsWith() 示例

content.startsWith('F') // true
content.startsWith('f') // false
content.startsWith('e', midContent) // true
Enter fullscreen mode Exit fullscreen mode

endsWith() 方法

endsWith()方法判断一个字符串是否以指定字符串的字符结尾。否则,它返回false

endsWith() 示例

content.endsWith('L', lastChar) // true
content.endsWith('l', lastChar) // false
Enter fullscreen mode Exit fullscreen mode

includes() 方法

includes()方法允许您确定一个字符串是否包含另一个字符串,并返回该字符串。Boolean

/**
 * @param  {string} string to search for
 * @param  {number} optional - index, defaults to 0
 * @return {boolean}
 */
string.includes(string, position)
Enter fullscreen mode Exit fullscreen mode

includes() 示例

content.includes('Ipsum') // true
content.includes('1960s') // true
content.includes('Hello from outside') // false
Enter fullscreen mode Exit fullscreen mode

repeat() 方法

repeat()方法构造并返回一个新字符串,该字符串由指定数量的被调用字符串的副本连接而成

/**
 * @param  {number} - indicating the number of times to repeat
 * @return {string}
 */
string.repeat(count)
Enter fullscreen mode Exit fullscreen mode

重复次数必须为:非负数,小于无穷大,且不超出字符串最大长度。

repeat() 示例

"**".repeat(3)  // "******"
"😺".repeat(3)  // "😺😺😺"
Enter fullscreen mode Exit fullscreen mode

总而言之,上述方法可以用其他方式实现,它们可能影响性能,也可能是最快的选择!最终结果将取决于您的需求。

要更详细地了解所有可用的属性和方法,
我强烈建议阅读完整的JavaScript 字符串参考文档

文章来源:https://dev.to/mahmoudelmahdi/javascript-string-methods-you-probously-havent-heard-of-and-thats-okay-23hc