Home  >  Article  >  Web Front-end  >  javascript charAt() 获取字符串指定位置的字符

javascript charAt() 获取字符串指定位置的字符

WBOY
WBOYOriginal
2016-06-01 09:54:392681browse

charAt 方法返回指定索引位置处的字符。

基本语法

<code class="language-javascript">strObj.charAt(index)</code>

参数介绍

参数 描述
index 必需。想得到的字符的基于零的索引。有效值是 0 与字符串长度减 1 之间的值。

说明

charAt 方法返回一个字符值,该字符位于指定索引位置。字符串中的第一个字符的索引为 0,第二个的索引为 1,等等。超出有效范围的索引值返回空字符串。

实例:

<code class="language-javascript"><script type="text/javascript">
var str="manongjc.com"
document.write(str.charAt(5)+"<br/>");
document.write(str.charAt(0)+"<br/>");
document.write(str.charAt(20)+"<br/>");
</script></code>

在线运行

运行结果:

<code>g
m</code>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn