簡介JavaScript中charAt()方法的使用
這個方法返回從指定索引的字符。
字符串中的字符進行索引從左向右。第一個字符的索引是0,并且在一個叫 stringName字符串的最后一個字符的索引是
stringName.length- 1。
語法
string.charAt(index);
| 參數(shù) | 描述 |
|---|---|
| index | 必需。表示字符串中某個位置的數(shù)字,即字符在字符串中的下標。 |
下面是參數(shù)的詳細信息:
- index: 介于0和1比串的長度以下的整數(shù)。
返回值:
返回從指定索引的字符。
提示和注釋
注釋:字符串中第一個字符的下標是 0。如果參數(shù) index 不在 0 與 string.length 之間,該方法將返回一個空字符串。
實例
在字符串 "Hello world!" 中,我們將返回位置 1 的字符:
<script type="text/javascript"> var str="Hello world!" document.write(str.charAt(1)) </script>
以上代碼的輸出是:
e
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(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>
這將產生以下結果:
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正則表達式之match函數(shù)講解
- JS正則中的match與exec使用說明
- javascript中match函數(shù)的用法小結
- js charAt的使用示例
- JavaScript charCodeAt方法入門實例(用于取得指定位置字符的Unicode編碼)
- js Map List 遍歷使用示例
- JS Map 和 List 的簡單實現(xiàn)代碼
- js實現(xiàn)的map方法示例代碼
- js正則表達式之search方法講解
- js中exec、test、match、search、replace、split用法
- JS常見疑難點分析之match,charAt,charCodeAt,map,search用法分析
相關文章
document.getElementBy("id")與$("#id")有什么區(qū)
有朋友問document.getElementBy("id")與$("#id")的區(qū)別,其實第一個就是js中獲取對象的方法, 第二個是通過自定義函數(shù)方便調用,而第三個是jquery中獲取id對象的方法2013-09-09
利用javascript數(shù)組長度循環(huán)數(shù)組內所有元素
javascript循環(huán)數(shù)組內所有元素代碼學習,大家參考使用吧2013-12-12

