- string.includes(value)
- 문자열에 value가 존재하는지 판별 후 참 또는 거짓 반환
const str = 'a santa at nasa' str.includes('santa') // true str.includes('asan') // false
- string.split(value)
value : 나누는 기준
- 없다 : string을 배열에 담아 반환
- 빈 문자열 : 한 문자씩 나눈 배열 반환
- 기타 문자열 : 해당 문자열로 나눈 배열 반환
const str = 'a cup' str.split() // ['a cup'] str.split('') // ['a', ' ', 'c', 'u', 'p'] str.split(' ') // ['a', 'cup']
-
string.replace(from, to)
-
문자열에 from 값이 존재할 경우, 1개만 to 값으로 교체하여 반환
-
string.replaceAll(from, to)
-
문자열 내의 모든 from을 to로 교체해 반환
const str = 'a b c d' str.replace(' ', '-') // a-b c d str.replaceAll(' ', '-') // a-b-c-d
- 문자열의 앞뒤의 공백문자 제거(' ', \t, \n)
- str.trim() : 시작과 끝 모두 제거
- str.trimStart() : 시작의 공백 제거
- str.trimEnd() : 끝의 공백 제거
const str = ' hello ' str.trim() // 'hello' str.trimStart() // 'hello ' str.trimEnd() // ' hello'
