简介JavaScript中charAt()⽅法的使⽤
这个⽅法返回从指定索引的字符。
字符串中的字符进⾏索引从左向右。第⼀个字符的索引是0,并且在⼀个叫 stringName字符串的最后⼀个字符的索引是stringName.length- 1。
语法
string.charAt(index);
参数描述
index必需。表⽰字符串中某个位置的数字,即字符在字符串中的下标。
下⾯是参数的详细信息:
index: 介于0和1⽐串的长度以下的整数。
返回值:
返回从指定索引的字符。
提⽰和注释
注释:字符串中第⼀个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该⽅法将返回⼀个空字符串。
实例
在字符串 "Hello world!" 中,我们将返回位置 1 的字符:
<script type="text/javascript">
var str="Hello world!"
document.write(str.charAt(1))
</script>
以上代码的输出是:
e
type="text/javascript"> var str="Hello world!" document.write("The first character is: " +
str.charAt(0) + "") document.write("The second character is: " + str.charAt(1) + "") document.write("The third character is: " + str.charAt(2))
</script>全选注:引⼊外部Js需再刷新⼀下页⾯才能执⾏]
例⼦:
<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
如何下载javascript<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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论