>  기사  >  웹 프론트엔드  >  JavaSacropt_Basic 지식 중 charCodeAt() 메소드 사용법에 대한 자세한 설명

JavaSacropt_Basic 지식 중 charCodeAt() 메소드 사용법에 대한 자세한 설명

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

이 메소드는 주어진 인덱스에 있는 문자의 유니코드 값을 나타내는 숫자를 반환합니다.

유니코드 코드 포인트 범위는 0부터 1114111까지입니다. 처음 128개 유니코드 코드 포인트의 ASCII 문자 인코딩과 직접 일치합니다. charCodeAt()는 항상 65,536보다 작은 값을 반환합니다.
문법

string.charCodeAt(index);

매개변수의 세부정보는 다음과 같습니다.

  • 인덱스: 문자열 길이보다 작은 0과 1 사이의 정수입니다. 지정하지 않으면 기본값은 0입니다.

반환 값:

주어진 인덱스에 있는 문자의 유니코드 값을 나타내는 숫자를 반환합니다. 주어진 인덱스의 길이가 0과 1 사이가 아닌 경우 NaN이 반환됩니다.
예:

<html>
<head>
<title>JavaScript String charCodeAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); 
document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); 
document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); 
document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); 
document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); 
document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5)); 
</script>
</body>
</html>

이렇게 하면 다음과 같은 결과가 나타납니다.

str.charCodeAt(0) is:84
str.charCodeAt(1) is:104
str.charCodeAt(2) is:105
str.charCodeAt(3) is:115
str.charCodeAt(4) is:32
str.charCodeAt(5) is:105 

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