인코딩의 Decoding

반응형

"test.file.txt"라는 String에서 확장자를 어떻게 추출할 수 있을까??

--> lastIndexOf()를 사용하여 '.'의 마지막 위치를 찾을 수 있고 그 뒤가 확장자 값이다.

이런 상황에서 '.'의 마지막 위치를 찾을 수 있는 lastIndexOf를 알아보자

문법
String.lastIndexOf([searchValue [,fromIndex])
String에서 searchValue의 마지막 위치의 시작 인덱스를 반환한다. fromIndex 반대 방향으로(생략가능)
ex)
const strValue = "test.file.txt";
strValue.lastIndexOf("."); // return 9

추가 예시)

"hello hello world".lastIndexOf("hello"); //return 6

"hello hello world".lastIndexOf("hello",3); //return 0 : index 3(l)에서 왼쪽편르러 search

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band