无涯教程-Javascript - charAt()函数

86 阅读1分钟

charat()是从指定索引返回字符的方法。

charAt() - 语法

使用以下语法查找特定索引处的字符。

string.charAt(index);

index  - 小于字符串长度的0到1之间的整数。

charAt() - 返回值

返回指定索引中的字符。

charAt() - 示例

<html>
   <head>
      <title>JavaScript String charAt() Method</title>
   </head>
   
   <body>  
      <script type="text/javascript">
         var str=new String( "This is string" );
         document.writeln("str.charAt(0) is:" + str.charAt(0)); 
         document.writeln("<br />str.charAt(1) is:" + str.charAt(1)); 
         document.writeln("<br />str.charAt(2) is:" + str.charAt(2)); 
         document.writeln("<br />str.charAt(3) is:" + str.charAt(3)); 
         document.writeln("<br />str.charAt(4) is:" + str.charAt(4)); 
         document.writeln("<br />str.charAt(5) is:" + str.charAt(5)); 
      </script>      
   </body>
</html>

运行上面代码输出

str.charAt(0) is:T 
str.charAt(1) is:h 
str.charAt(2) is:i 
str.charAt(3) is:s 
str.charAt(4) is: 
str.charAt(5) is:i 

参考链接

www.learnfk.com/javascript/…