본문 바로가기

아래로 스크롤 해주세요!

My Reference Book

-

제가 배웠던것을 한곳에 정리해보았어요!

HTML

HTML 태그 톺아보기

HTML

자세히보기

CSS

CSS 속성 톺아보기

CSS

자세히보기

JAVASCRIPT

JS 실행문 톺아보기

JAVASCRIPT

자세히보기

최신댓글

Javascript

IndexOf()/lastIndexOf() 에 대하여!

by C0Di 2022. 8. 16.
728x90

indexOf

문자열에서 특정 문자의 위치를 찾고 그 값이 일치했을때 숫자를 반환하며 일치하는 값이 없을때는 -1을 반홥합니다.

::예시

"문자열".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

lastIndexOf

lastIndexOf()는 indexOf()와 달리 뒤에서 부터 특정 문자를 찾습니다.

::예시

"문자열".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
728x90
반응형

댓글

#HASH_TAGS

-

1

선택해주세요 오징어 1Kg 당 3000원 오늘은 내가바로 오징어! ImageSlideEffect 제이쿼리 테스트 이건 또 뭐람 오징어 두마리 포획 완료 scroll-snap-type 다크모드 오징어 한마리 수확 완료! scroll-snap-align 오늘도 웹표준은.. 필터선택자 코딩 화사한가요? 테스트테스트 JQuery 숙제가 다양해서 너무 좋아요 오늘 조업 마감했습니다. HTML 울적하니 꽃을 달아봤습니다 Method 코드 공부 내일은 즐거운 월요일 슬라이드 결과 : 월요일 메서드 멈추지 않는 ' j ' 시리-즈