Javascript String.charAt()方法

這個方法返回從指定索引的字元。

字串中的字元進行索引從左向右。第一個字元的索引是0,並且在一個叫 stringName字串的最後一個字元的索引是stringName.length- 1。

語法

string.charAt(index);

下麵是參數的詳細資訊:

  • index: 介於0和1比串的長度以下的整數。

返回值:

返回從指定索引的字元。

例子:

<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

 


上一篇: Javascript String.length屬性 下一篇: Javascript String.charCodeAt()方法