본문 바로가기
Javascript

charAt()

by 코터틀 2022. 8. 22.
반응형

문자열 메서드 : charAt()

match() 메서드는 문자열에서 특정 인덱스에 위치하는 유니코드 단일문자를 반환합니다.

"문자열".charAt("위치값");
const str1 = "javascript reference";

const currentStr1 = str1.charAt(); // j
const currentStr2 = str1.charAt("0"); // j
const currentStr3 = str1.charAt("1"); // a
const currentStr4 = str1.charAt("2"); // v

charAt() 예제

var anyString = 'Brave new world';
console.log("The character at index 0   is '" + anyString.charAt()   + "'");
// No index was provided, used 0 as default

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
결과 확인하기
The character at index 0 is 'B'
The character at index 1 is 'r'
The character at index 2 is 'a'
The character at index 3 is 'v'
The character at index 4 is 'e'
The character at index 999 is ''
반응형

'Javascript' 카테고리의 다른 글

요소 크기 메서드  (12) 2022.09.01
GSAP  (9) 2022.08.29
match()  (6) 2022.08.22
search()  (6) 2022.08.22
함수 유형  (4) 2022.08.22

댓글


광고 준비중입니다.