您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

简介JavaScript中charAt()方法的使用

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了JavaScript中charAt()方法的使用详解,是JS入门学习中的基础知识,需要的朋友可以参考下

  这个方法返回从指定索引的字符。

  字符串中的字符进行索引从左向右。第一个字符的索引是0,并且在一个叫 stringName字符串的最后一个字符的索引是stringName.length- 1。

  语法

  ?

1 string.charAt(index);

  下面是参数的详细信息:

  index: 介于0和1比串的长度以下的整数。

  返回值:

  返回从指定索引的字符。

  例子:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <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>

  这将产生以下结果:

  ?

1 2 3 4 5 6 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