JavaScript中的String对象研究02:实例方法——at、charAt、charCodeAt、codePointAt、concat、endsWith、includes、indexOf
在JavaScript中,String
对象提供了丰富的实例方法。本篇文章将深入探讨以下常用的String
实例方法:
at()
charAt()
charCodeAt()
codePointAt()
concat()
endsWith()
includes()
indexOf()
一、at()
1. 定义
at()
方法根据指定的索引,返回字符串中对应的字符。它支持正负索引,负索引用于从字符串末尾倒数。
2. 语法
str.at(index)
index
:要查找的字符的索引,可以是正整数或负整数。
3. 用途
- 获取字符串中特定位置的字符。
- 支持负索引,更方便地从字符串末尾获取字符。
4. 示例
let str = "Hello, World!";
console.log(str.at(0)); // "H"
console.log(str.at(7)); // "W"
console.log(str.at(-1)); // "!"
console.log(str.at(-5)); // "o"
5. 注意事项
at()
方法是ES2022(ES13)引入的,旧版本的浏览器或运行环境可能不支持。- 与数组的
at()
方法类似,也支持负索引。
二、charAt()
1. 定义
charAt()
方法返回字符串中指定位置的字符。
2. 语法
str.charAt(index)
index
:介于0
到str.length - 1
之间的整数。
3. 用途
- 获取字符串中特定位置的字符。
- 不支持负索引。
4. 示例
let str = "Hello, World!";
console.log(str.charAt(0)); // "H"
console.log(str.charAt(7)); // "W"
console.log(str.charAt(20)); // ""(索引超出范围,返回空字符串)
5. 注意事项
- 如果
index
超出范围,charAt()
将返回空字符串。 - 不支持负索引,如果需要从末尾获取字符,需要计算正索引。
三、charCodeAt()
1. 定义
charCodeAt()
方法返回字符串中指定位置字符的UTF-16代码单元值(0 到 65535 之间的整数)。
2. 语法
str.charCodeAt(index)
index
:介于0
到str.length - 1
之间的整数。
3. 用途
- 获取字符串中特定位置字符的Unicode码点(对于基本多文种平面字符)。
- 用于字符编码转换、字符比较等。
4. 示例
let str = "ABCabc";
console.log(str.charCodeAt(0)); // 65 ('A'的码点)
console.log(str.charCodeAt(3)); // 97 ('a'的码点)
console.log(str.charCodeAt(6)); // NaN (超出范围,返回NaN)
5. 注意事项
- 对于代理对(surrogate pair)组成的字符(如Emoji等超出基本多文种平面BMP的字符),
charCodeAt()
只能获取第一个代码单元,需要使用codePointAt()
。
四、codePointAt()
1. 定义
codePointAt()
方法返回字符串中指定位置字符的Unicode码点(支持大于0xFFFF的字符)。
2. 语法
str.codePointAt(position)
position
:介于0
到str.length - 1
之间的整数。
3. 用途
- 获取完整的Unicode码点,支持代理对字符。
- 适用于处理Emoji等特殊字符。
4. 示例
let str = "𝟘𝟙𝟚ABC";
console.log(str.codePointAt(0)); // 120792 ('𝟘'的码点)
console.log(str.codePointAt(1)); // 120793 ('𝟙'的码点)
console.log(str.codePointAt(4)); // 65 ('A'的码点)
5. 注意事项
codePointAt()
返回的是完整的Unicode码点。- 对于代理对字符,占用两个代码单元,索引需要正确计算。
五、concat()
1. 定义
concat()
方法将一个或多个字符串拼接起来,返回一个新的字符串。
2. 语法
str.concat(string2[, string3, ..., stringN])
string2, ..., stringN
:要拼接的字符串。
3. 用途
- 拼接多个字符串,生成新的字符串。
4. 示例
let str1 = "Hello";
let str2 = "World";
let result = str1.concat(", ", str2, "!");
console.log(result); // "Hello, World!"
5. 注意事项
-
concat()
不会修改原字符串,返回新的字符串。 -
也可以使用加号
+
或模板字符串进行拼接:let result = str1 + ", " + str2 + "!"; // 或 let result = `${str1}, ${str2}!`;
六、endsWith()
1. 定义
endsWith()
方法用于判断当前字符串是否以指定的子字符串结尾,根据判断结果返回true
或false
。
2. 语法
str.endsWith(searchString[, length])
searchString
:要搜索的子字符串。length
:可选,用于截取字符串的长度。
3. 用途
- 检查字符串是否以某个子字符串结尾。
- 可用于验证文件扩展名、URL路径等。
4. 示例
let filename = "document.pdf";
console.log(filename.endsWith(".pdf")); // true
console.log(filename.endsWith(".doc")); // false
// 使用length参数
let str = "Hello, World!";
console.log(str.endsWith("Hello", 5)); // true
5. 注意事项
length
参数用于指定字符串的长度,在此长度内进行检查。- 方法区分大小写。
七、includes()
1. 定义
includes()
方法用于判断一个字符串是否包含另一个子字符串,根据结果返回true
或false
。
2. 语法
str.includes(searchString[, position])
searchString
:要搜索的子字符串。position
:可选,开始搜索的位置,默认值为0
。
3. 用途
- 检查字符串中是否存在某个子字符串。
- 用于过滤、搜索等功能。
4. 示例
let text = "The quick brown fox jumps over the lazy dog";
console.log(text.includes("fox")); // true
console.log(text.includes("cat")); // false
console.log(text.includes("The", 1)); // false (从位置1开始搜索)
5. 注意事项
- 方法区分大小写。
- 如果
searchString
是空字符串,includes()
总是返回true
。
八、indexOf()
1. 定义
indexOf()
方法返回指定子字符串在字符串中首次出现的位置,如果未找到则返回-1
。
2. 语法
str.indexOf(searchValue[, fromIndex])
searchValue
:要搜索的子字符串。fromIndex
:可选,开始搜索的位置,默认值为0
。
3. 用途
- 查找子字符串在字符串中的位置。
- 判断子字符串是否存在,以及出现的位置。
4. 示例
let text = "The quick brown fox jumps over the lazy dog";
console.log(text.indexOf("quick")); // 4
console.log(text.indexOf("the")); // 31 (注意大小写)
console.log(text.indexOf("cat")); // -1
console.log(text.indexOf("o", 12)); // 17 (从位置12开始搜索)
5. 注意事项
-
方法区分大小写。
-
如果子字符串未找到,返回
-1
。 -
可以结合
while
循环查找所有出现的位置。let positions = []; let pos = text.indexOf("o"); while (pos !== -1) { positions.push(pos); pos = text.indexOf("o", pos + 1); } console.log(positions); // [12, 17, 41]
九、总结
at()
:支持正负索引,获取指定位置的字符。charAt()
:获取指定位置的字符,不支持负索引。charCodeAt()
:获取指定位置字符的UTF-16代码单元值。codePointAt()
:获取指定位置字符的完整Unicode码点,支持代理对字符。concat()
:拼接多个字符串,返回新字符串。endsWith()
:判断字符串是否以指定子字符串结尾。includes()
:判断字符串是否包含指定子字符串。indexOf()
:查找子字符串首次出现的位置。
十、参考资料
- MDN Web Docs - String
- MDN Web Docs - String.prototype.at()
- MDN Web Docs - String.prototype.charAt()
- MDN Web Docs - String.prototype.charCodeAt()
- MDN Web Docs - String.prototype.codePointAt()
- MDN Web Docs - String.prototype.concat()
- MDN Web Docs - String.prototype.endsWith()
- MDN Web Docs - String.prototype.includes()
- MDN Web Docs - String.prototype.indexOf()