簡(jiǎn)介JavaScript中charAt()方法的使用
這個(gè)方法返回從指定索引的字符。
字符串中的字符進(jìn)行索引從左向右。第一個(gè)字符的索引是0,并且在一個(gè)叫 stringName字符串的最后一個(gè)字符的索引是
stringName.length- 1。
語法
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
- js正則函數(shù)match、exec、test、search、replace、split使用介紹集合
- js正則表達(dá)式之match函數(shù)講解
- JS正則中的match與exec使用說明
- javascript中match函數(shù)的用法小結(jié)
- js charAt的使用示例
- JavaScript charCodeAt方法入門實(shí)例(用于取得指定位置字符的Unicode編碼)
- js Map List 遍歷使用示例
- JS Map 和 List 的簡(jiǎn)單實(shí)現(xiàn)代碼
- js實(shí)現(xiàn)的map方法示例代碼
- js正則表達(dá)式之search方法講解
- js中exec、test、match、search、replace、split用法
- JS常見疑難點(diǎn)分析之match,charAt,charCodeAt,map,search用法分析
相關(guān)文章
javascript學(xué)習(xí)筆記(十二) RegExp類型介紹
javascript學(xué)習(xí)筆記之RegExp類型介紹,學(xué)習(xí)js的朋友可以參考下2012-06-06
JavaScript高級(jí)程序設(shè)計(jì) 學(xué)習(xí)筆記 js高級(jí)技巧
JavaScript高級(jí)程序設(shè)計(jì) 學(xué)習(xí)筆記 js高級(jí)技巧,學(xué)習(xí)js的朋友可以參考下。2011-09-09
javascript 數(shù)組的定義和數(shù)組的長(zhǎng)度
本文主要介紹javascript 數(shù)組的定義和數(shù)組的長(zhǎng)度,比較簡(jiǎn)單,希望能給大家做一個(gè)參考。2016-06-06
javascript中實(shí)現(xiàn)兼容JAVA的hashCode算法代碼分享
這篇文章主要介紹了javascript中實(shí)現(xiàn)兼容JAVA的hashCode算法代碼分享,實(shí)現(xiàn)跟JAVA中的運(yùn)算結(jié)果一致,需要的朋友可以參考下2014-08-08
DOM下的節(jié)點(diǎn)屬性和操作小結(jié)
DOM 節(jié)點(diǎn)屬性操作方法小結(jié)。2009-05-05
javascript 內(nèi)置對(duì)象及常見API詳細(xì)介紹
這篇文章主要介紹了javascript 內(nèi)置對(duì)象及常見API的相關(guān)資料,這里對(duì)內(nèi)置對(duì)象進(jìn)行了詳細(xì)的整理,需要的朋友可以參考下2016-11-11
在JavaScript的正則表達(dá)式中使用exec()方法
這篇文章主要介紹了在JavaScript的正則表達(dá)式中使用exec()方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06

