JavaScript 中 charAt() 方法有什么用?
javascriptobject oriented programmingprogramming
charAt()
charAt() 方法有助于在特定索引处的字符串中查找字符。它需要一个参数(索引)来执行与特定索引相对应的字符。
语法
string.charAt(index);
示例 1
在以下示例中,当索引 9 作为参数传递给 charAt() 方法时,将显示输出 p。
<html> <body> <p id="character"></p> <script> var string = "TUTORIALSPOINT AND TUTORIX"; var result = string.charAt(9) document.getElementById("character").innerHTML = result; </script> </body> </html>
输出
p
示例 2
在下面的示例中,当索引 11 作为参数传递时,charAt() 方法将字符 a 作为输出执行
<html> <body> <script> var string = "Tutorix is a best e-learning platform"; var result = string.charAt(11) document.write(result); </script> </body> </html>
输出
a