본문 바로가기

FRONT

[JavaScript 자바스크립트] indexOf 란 ?

728x90
string.indexOf(searchvalue, position)

 

- 문자열(string)에서 특정 문자열(searchvalue )을 찾고, 검색된 문자열이 첫번째로 나타나는 위치 index를 리턴(posiotion)

 

- 파라미터

  • searchvalue : 필수 입력값, 찾을 문자열
  • posiotion : 기본값은 0, string에서 searchvalue를 찾기 시작할 위치

- 찾는 문자열이 없으면 -1을 리턴

 

- 문자열을 찾을 때 대소문자 구분

 

 

기본 예제

 

1) 문자열 'abab'에서 'ab'가 처음으로 나타나는 위치의 인덱스 값을 리턴

2) 문자열 'abab'에서 'ba'가 처음으로 나타나는 위치의 인덱스 값을 리턴

3) 문자열 'abab'에는 'abc'라는 문자열이 없으므로 숫자 -1을 리턴

4) indexOf 함수는 대소문자를 구분. 문자열 'abab'에는 대문자 'AB'는 없으므로 숫자 -1을 리턴

 

const name = "abab";

document.writeln(str.indexOf('ab')); // 0
document.writeln(str.indexOf('ba')); // 1
document.writeln(str.indexOf('abc')); // -1
document.writeln(str.indexOf('AB')); // -1

 

 

position 값을 입력한 예제

 

1) indexOf 함수의 두번째 파라미터인 position값이 입력되지 않으면, position의 값은 0으로 처리.

위 예제에서는 'abab' 문자열의 0번째 index부터 'ab' 문자열을 찾고, 0번째 index에서 문자열 'ab'를 발견하였으므로,

0을 리턴

2) position 값을 '1' 로 입력.

위 예제에서는 'abab' 문자열의 1번째 index부터 'ab' 문자열을 검색하므로, 
index 0에 있는 'ab'는 무시

 

const name = "abab";

document.writeln(str.indexOf('ab')); // 0
document.writeln(str.indexOf('ab', 1)); // 2

 

 

문자열에 있는 모든 searchvalue 위치 찾는 예제

 

반복문 안에서 searchvalue를 찾고 나서(findPos), position 값을 foundPos의 다음 index값으로 변경 후

더 이상 문자열을 찾지 못하면 반복문을 종료

 

let name = 'abcabcabc';
let searchvalue = 'ab'; 

let pos = 0;
while (true) {
  let findPos = name.indexOf(searchvalue, pos);
  if (findPos == -1) break;

  document.writeln( findPos ); //0 3 6
  pos = findPos + 1; 
}
728x90