티스토리 뷰
주소 경로를 구분하기 위해 사용한 endswith메서드에 대해 정리해본다.
const cleanedPathname = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
1. endsWith()
문자열.endsWith()
str.endsWith(searchString[, length])
String 값의 endsWith() 메서드는 문자열이 이 문자열의 문자로 끝나는지 여부를 결정하여 적절하게 true 또는 false를 반환합니다.
문자열 인스턴스의 끝 부분과 지정한 문자열이 일치하는지를 확인합니다.
예제) endsWith() 사용하기
const str = "To be, or not to be, that is the question.";
console.log(str.endsWith("question.")); // true
console.log(str.endsWith("to be")); // false
console.log(str.endsWith("to be", 19)); // true
예시)
const str = "Tom is thinking";
const action = "thinking";
console.log(str.endsWith(action)); // true
const str = "Tom is thinking";
const action = "ing";
console.log(str.endsWith(action)); // true
문자열이 thinking으로 끝난다. ing로 끝난다. 모두 참이므로 true를 반합니다.
const str = "Tom is thinking";
const action = "dancing";
console.log(str.endsWith(action)); // false
dancing으로 끝나지 않으므로 false를 반환합니다.
const str = "Tom is thinking";
console.log(str.endsWith("thinking", 6)); // false
길이 6에서 자른 문자열("Tom is")은 thinking으로 끝나지 않으므로 false를 반환합니다.
const str = "Tom is thinking";
console.log(str.endsWith("thinking", 12)); // false
길이 12에서 자른 문자열("Tom is think")이 thinking으로 끝나지 않으므로 false를 반환합니다.
const str = "Tom is thinking";
console.log(str.endsWith("think", 12)); // true
길이 12에서 자른 문자열("Tom is think")이 think로 끝나므로 true를 반환합니다.
2. startsWith
startsWith(searchString)
startsWith(searchString, position)
문자열이 searchString으로 시작하면 true, 아니면 false를 반환합니다. 두 번째 인자로 position값을 전달하면 탐색할 시작위치를 정할 수 있습니다. 기본값은 0입니다.
예시)
const str = "Tom is thinking";
const name = "Tom";
console.log(str.startsWith(name)); // true
const str = "Tom is thinking";
const name = "T";
console.log(str.startsWith(name)); // true
문자열이 Tom으로 시작한다. T로 시작한다. 모두 참이므로 true를 반환합니다.
const str = "Tom is thinking";
const name = "Jerry";
console.log(str.startsWith(name)); // false
Jerry로 시작하지 않으므로 false를 반환합니다.
두 번째 인자를 활용하는 경우
const str = "Tom is thinking";
console.log(str.startsWith("is", 4)); // true
4번째 인덱스부터 탐색을 시작하면 is로 시작하므로 true를 반환합니다.
const str = "Tom is thinking";
console.log(str.startsWith("is", 3)); // false
3번째 인덱스부터 탐색을 시작하면 공백(" ")으로 시작하므로 false를 반환합니다.
출처
https://learn.microsoft.com/ko-kr/dotnet/api/system.string.endswith?view=net-8.0
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
https://codingbroker.tistory.com/71
'JavaScript' 카테고리의 다른 글
[JS] reduce 메서드 (0) | 2024.09.25 |
---|---|
[비동기]Promise와 async/await의 차이 (2) | 2024.09.20 |
[Javascript] exec() 메서드란? (+ Match 함수 / MatchAll 함수) (4) | 2024.09.09 |
[노마드 바닐라JS 챌린지] Day.13 Geolocation (0) | 2023.05.30 |
[노마드 바닐라JS 챌린지] Day.12 ToDo (0) | 2023.05.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Cleanup
- 오블완
- TypeScript
- slice
- Async
- props
- nomard
- 리액트네이티브
- overloading
- 프로젝트회고록
- 프로그래머스
- await
- 노마드
- ReactJS
- ts
- 챌린지1일차
- useEffect
- NPM
- 티스토리챌린지
- useState
- splice
- create react app
- React
- 자바스크립트
- 타입스크립트
- 재귀함수
- 오버로딩
- 리액트
- CLI
- 카카오로그인
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함