Usage of JavaScript’s built-in function charAt()

charAt() is a built-in function of JavaScript that is used to get the character at a specified position in a string.
The basic syntax is as follows:

str.charAt(index);

Among them, str represents the string of characters to be intercepted, and index represents the position of the character to be obtained in the string, which is an integer starting from 0. If the index exceeds the range of the string, an empty string is returned ' '
For example: Suppose there is a string "hello" and you want to get its second character 'e', ​​you can use the charAt() function as follows:

const str = 'hello';
const char = str.charAt(1);
console.log(char); //输出'e'

In the above example, str.charAt(1) returns the character 'e' at position 1 in the string str.
It should be noted that if you need to get the character at the end of the string, there is another easier way, which is to use str.charAt(str .length -
1) directly without explicitly calculating the index position.

Guess you like

Origin blog.csdn.net/weixin_56733569/article/details/130729091