>  기사  >  웹 프론트엔드  >  JavaScript_Basic 지식 중 charAt() 메소드 사용 소개

JavaScript_Basic 지식 중 charAt() 메소드 사용 소개

WBOY
WBOY원래의
2016-05-16 15:56:171876검색

이 메서드는 지정된 인덱스의 문자를 반환합니다.

문자열의 문자는 왼쪽에서 오른쪽으로 색인이 지정됩니다. stringName이라는 문자열에서 첫 번째 문자의 인덱스는 0이고 마지막 문자의 인덱스는 stringName.length- 1입니다.
문법

string.charAt(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 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.