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

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

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

stringName.length- 1。

语法

string.charAt(index);

参数

描述

index

必需。表示字符串中某个位置的数字,即字符在字符串中的下标。

下面是参数的详细信息:

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

返回值:

返回从指定索引的字符。

提示和注释

注释:字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,该方法将返回一个空字符串。

实例

在字符串 "Hello world!" 中,我们将返回位置 1 的字符:

var str="Hello world!"

document.write(str.charAt(1))

以上代码的输出是:

e

例子:

JavaScript String charAt() Method

var str = new String( "This is string" );

document.writeln("str.charAt(0) is:" + str.charAt(0));

document.writeln("
str.charAt(1) is:" + str.charAt(1));

document.writeln("
str.charAt(2) is:" + str.charAt(2));

document.writeln("
str.charAt(3) is:" + str.charAt(3));

document.writeln("
str.charAt(4) is:" + str.charAt(4));

document.writeln("
str.charAt(5) is:" + str.charAt(5));

这将产生以下结果:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值