JavaScript 字符串 API 列表
API |
描述 |
at() |
方法接受一个整数值,并返回一个新的 String,该字符串由位于指定偏移量处的单个 UTF-16 码元组成。该方法允许正整数和负整数。负整数从字符串中的最后一个字符开始倒数。 |
charAt() |
返回一个由给定索引处的单个 UTF-16 码元构成的新字符串符,如果 index 超出了 0 – str.length - 1 的范围,charAt() 将返回一个空字符串 |
at
MDN 资料
const sentence = "The quick brown fox jumps over the lazy dog.";
let index = 5;
console.log(
`Using an index of ${index} the character returned is ${sentence.at(index)}`
);
index = -4;
console.log(
`Using an index of ${index} the character returned is ${sentence.at(index)}`
);
charAt
MDN 资料
const sentence = "The quick brown fox jumps over the lazy dog.";
const index = 4;
console.log(`The character at index ${index} is ${sentence.charAt(index)}`);