欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)介JavaScript中charAt()方法的使用

 更新時(shí)間:2020年05月10日 12:09:46   投稿:goldensun  
這篇文章主要介紹了JavaScript中charAt()方法的使用詳解,是JS入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

 這個(gè)方法返回從指定索引的字符。

字符串中的字符進(jìn)行索引從左向右。第一個(gè)字符的索引是0,并且在一個(gè)叫 stringName字符串的最后一個(gè)字符的索引是

stringName.length- 1。

語(yǔ)法

string.charAt(index);

參數(shù) 描述
index 必需。表示字符串中某個(gè)位置的數(shù)字,即字符在字符串中的下標(biāo)。

下面是參數(shù)的詳細(xì)信息:

  •     index: 介于0和1比串的長(zhǎng)度以下的整數(shù)。

返回值:

返回從指定索引的字符。

提示和注釋

注釋:字符串中第一個(gè)字符的下標(biāo)是 0。如果參數(shù) index 不在 0 與 string.length 之間,該方法將返回一個(gè)空字符串。

實(shí)例

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

<script type="text/javascript">
var str="Hello world!"
document.write(str.charAt(1))
</script>

以上代碼的輸出是:

e


[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]

例子:

<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>

這將產(chǎn)生以下結(jié)果:

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 

相關(guān)文章

最新評(píng)論