在 JavaScript 中获取字符串的第 N 个字符

在 JavaScript 中获取字符串的第 N 个字符

Get the Nth Character in a String using JavaScript

使用该charAt()方法获取字符串中的第 n 个字符,例如
str.charAt(1)返回字符串中的第二个字符。

该方法采用的唯一参数charAt是要返回的字符的索引。

索引.js
const str = 'bobbyhadz.com'; // ✅ Using charAt console.log(str.charAt(0)); // 👉️ "b" console.log(str.charAt(1)); // 👉️ "o" console.log(str.charAt(2)); // 👉️ "b" // ✅ Counting backwards console.log(str.charAt(str.length - 1)); // 👉️ "m"

String.charAt方法采用和之间整数并返回相应的字符。0string.length - 1

如果字符串中不存在索引,则该方法返回一个空字符串。

JavaScript 索引是从零开始的,所以字符串中第一个字符的索引是0,最后一个字符的索引是 string.length - 1

当越界传递索引时,该String.charAt()方法返回一个空字符串。

索引.js
console.log(''.charAt(5)); // 👉️ ""

您可以通过从字符串的长度中减去字符数来倒数。

例如,string.length - 2给我们字符串中的倒数第二个字符。

索引.js
const str = 'bobbyhadz.com'; // ✅ Counting backwards console.log(str.charAt(str.length - 2)); // 👉️ "o" console.log(str.charAt(str.length - 1)); // 👉️ "m"

使用括号表示法获取字符串的第 N 个字符

另一种方法是使用括号表示法。

当指定不存在的索引时,undefined返回。

索引.js
const str = 'bobbyhadz.com'; // ✅ Using bracket notation console.log(str[0]); // 👉️ "b" console.log(str[1]); // 👉️ "o" console.log(str[2]); // 👉️ "b" // ✅ Count backwards console.log(str[str.length - 1]); // 👉️ "m"
您最常会看到括号[]表示法用于访问特定索引处的字符串。

括号表示法方法与方法不同,因为它在提供字符串中不存在的索引时String.charAt()返回。undefined

索引.js
console.log(''[5]); // 👉️ undefined

You can also use bracket notation to count backward, simply subtract the number
of characters from the string’s length.

index.js
const str = 'bobbyhadz.com'; console.log(str[str.length - 1]); // 👉️ "m" console.log(str[str.length - 2]); // 👉️ "o" console.log(str[str.length - 3]); // 👉️ "c"

The String.charAt() method is quite convenient when you need to have a
guarantee that a string is going to be returned even if the index is out of
bounds.

Dealing with possibly undefined values might make your code more complicated
than it needs to be.

# Get the Nth Character of a String using String.at()

You can also use the String.at() method.

For example, String.at(1) returns the second character in the string.

index.js
const str = 'bobbyhadz.com'; console.log(str.at(0)); // 👉️ b console.log(str.at(1)); // 👉️ o // ✅ count backward console.log(str.at(-1)); // 👉️ m console.log(str.at(-2)); // 👉️ o console.log(str.at(-3)); // 👉️ c

The
String.at
method takes an integer and returns a new string containing the character at the
specified position.

The String.at() method allows for positive and negative integers.

You can use negative integers to count back from the end of the string.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.at(0); console.log(firstChar); // 👉️ b console.log(str.at(-1)); // 👉️ m console.log(str.at(-2)); // 👉️ o console.log(str.at(-3)); // 👉️ c

The String.at() method is quite convenient when you need to count backward.

The String.charAt() method and bracket notation approach do not support
negative indexing. The String.at() method returns undefined when the
specified index doesn’t exist in the string.

index.js
const str = ''; console.log(str.at(0)); // 👉️ undefined

Both String.at() and bracket notation return undefined when accessing an
index out of bounds.

The String.charAt() method is the only option that returns an empty string if
you access an index that doesn’t exist.

# Additional Resources

You can learn more about the related topics by checking out the following
tutorials: