Animated Rainbow Nyan Cat
본문 바로가기
javascript

indexOf() / lastIndexOf

by 이유나1 2022. 8. 16.
728x90
반응형

indexOf()

문자열에서 특정 문자의 취치를 찾고 숫자를 반환합니다.
"문자열, indexOf(검색값)"
"문자열, indexOf(검색값, 위치값) "
//0 1 2 3 4 5 6 7 8 9 10 11
const str1 = "javascript reference"
const currentStr1 = str1.indexOf("javascript"); //0
const currentStr2 = str1.indexOf("reference"); //11
const currentStr3 = str1.indexOf("j"); //0
const currentStr4 = str1.indexOf("a"); //1
const currentStr5 = str1.indexOf("v"); //2
const currentStr6 = str1.indexOf("jquery"); //-1 데이ㅓ가 없으면-1로 나온다.
const currentStr7 = str1.indexOf("b"); //-1 
const currentStr8 = str1.indexOf("javascript", 0); //0 
const currentStr9 = str1.indexOf("javascript", 1); //-1
const currentStr10 = str1.indexOf("reference", 0); //11
const currentStr11 = str1.indexOf("reference", 1); //11
const currentStr12 = str1.indexOf("reference", 11); //11
const currentStr13 = str1.indexOf("reference", 12); //-1   

lastIndexOf()

문자열에서 특정 문자의 취치를 찾고 숫자를 반환합니다.
"문자열, lastIndexOf(검색값)"
"문자열, lastIndexOf(검색값, 위치값) "
//11 10 9 8 7 6 5 4 3 2 1 0
const str2 = "javascript reference"

const currentStr14 = str1.lastIndexOf("javascript");    //0 
const currentStr15 = str1.lastIndexOf("reference");     //11
const currentStr16 = str1.lastIndexOf("j");             //0
const currentStr17 = str1.lastIndexOf("a");             //3 순서가 뒤에서 기준?
const currentStr18 = str1.lastIndexOf("v");             //2 
const currentStr19 = str1.lastIndexOf("jquery");        //-1
const currentStr20 = str1.lastIndexOf("b");             //-1
const currentStr21 = str1.lastIndexOf("javascript", 0); //0 
const currentStr22 = str1.lastIndexOf("javascript", 1); //0
const currentStr23 = str1.lastIndexOf("reference", 0);  //-1
const currentStr24 = str1.lastIndexOf("reference", 1);  //-1
const currentStr25 = str1.lastIndexOf("reference", 11); //11
const currentStr26 = str1.lastIndexOf("reference", 12); //11  
728x90

'javascript' 카테고리의 다른 글

소문자/대문자/공백  (2) 2022.08.17
문자열 결합 / 템플릿 문자열  (2) 2022.08.17
slice()/substring()/substr()  (4) 2022.08.16
정규식 표현  (3) 2022.08.16
내장함수  (1) 2022.08.14

댓글


/
/
/

CSS
광고준비중입니다.