728x90
indexOf
문자열에서 특정 문자의 위치를 찾고 그 값이 일치했을때 숫자를 반환하며 일치하는 값이 없을때는 -1을 반홥합니다.
::예시
"문자열".indexOf(검색값)
"문자열".indexOf(검색값, 위치값)
"문자열".indexOf(검색값)
"문자열".indexOf(검색값, 위치값)
const str1 = "javascript reference";
const currentStr1 = str1.indexOf("javascript"); //indexOf는 찾는값의 첫번째 자리 시작값 0 : 답 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"); //indexOf는 데이터가 없을때 : 답 -1
const currentStr7 = str1.indexOf("b"); //indexOf는 데이터가 없을때 : 답 -1
const currentStr8 = str1.indexOf("javascript, 0"); // 0번째 자리에 있는지 : 답 0
const currentStr9 = str1.indexOf("javascript, 1"); // 1번째 자리에 있는지 : 답 -1
const currentStr10 = str1.indexOf("reference, 0"); // 0번째 자리에 있는지 : 답 11
const currentStr11 = str1.indexOf("reference, 1"); // 1번째 자리에 있는지 : 답 11
const currentStr12 = str1.indexOf("reference, 11"); // 11번째 자리에 있는지 : 답 11
const currentStr13 = str1.indexOf("reference, 12"); // 12번째 자리에 있는지 : 답 -1
결과 보기
0
11
0
1
2
-1
-1
0
-1
11
11
11
-1
11
0
1
2
-1
-1
0
-1
11
11
11
-1
lastIndexOf
lastIndexOf()는 indexOf()와 달리 뒤에서 부터 특정 문자를 찾습니다.
::예시
"문자열".lastIndexOf(검색값)
"문자열".lastIndexOf(검색값, 위치값)
"문자열".lastIndexOf(검색값)
"문자열".lastIndexOf(검색값, 위치값)
const str1 = "javascript reference";
const currentStr14 = str1.lastIndexOf("javascript"); // 0번째부터 값이 있고 있으니까 맨앞의 0이 나옵니다.
const currentStr15 = str1.lastIndexOf("reference"); // 11이 나옵니다
const currentStr16 = str1.lastIndexOf("j"); // 맨 앞에 j가 있으니 0이 나옵니다.
const currentStr17 = str1.lastIndexOf("a"); // 맨 앞에 a가 있으니 j(1), a(중복), v(2), a(3) 그래서 3이 나옵니다.
const currentStr18 = str1.lastIndexOf("v"); // 맨 앞에 v가 있으니 2이 나옵니다.
const currentStr19 = str1.lastIndexOf("jquery"); // 맨 앞에 v가 있으니 -1이 나옵니다.
const currentStr20 = str1.lastIndexOf("b"); // 맨 앞에 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
결과 보기
0
11
0
3
2
-1
-1
0
0
-1
-1
11
11
11
0
3
2
-1
-1
0
0
-1
-1
11
11
728x90
반응형
'Javascript' 카테고리의 다른 글
padStart()/padEnd()에 대하여! (2) | 2022.08.17 |
---|---|
정규식 표현에 대해 araboza! (4) | 2022.08.16 |
slice() / substring() / substr() 에 대하여~~ (6) | 2022.08.16 |
내장함수가 뭘까? (5) | 2022.08.14 |
join() / pop() / push() 에 대하여 알아보자! (9) | 2022.08.11 |
댓글