2023.04.04
- Set
const arr = [1,2,2,2,3,3,4];
const mySet = new Set(arr);
mySet.has(1);
mySet.has(0);
console.log([...mySet]);
const mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");
mySet.add(1);
mySet.size;
- charAt - 해당 index 문자열 반환
구문: str.charAt(index)
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.charAt(0));
- substring - 시작 인덱스로 부터 종료 인덱스 전 까지 문자열의 부분 문자열을 반환
구문: str.substring(indexStart[, indexEnd])
const str = 'Mozilla';
console.log(str.substring(1, 3));
console.log(str.substring(2));
- indexOf - string 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환, 없으면 -1 반환
💡 indexOf를 사용하여 문자열 내의 특정 문자 숫자 세기
const s = "110010101001";
let str = s.indexOf("1");
let cnt = 0;
while(str !== -1){
cnt++;
str = s.indexOf("1", str+1);
}
console.log(cnt);
- toString - 진수 변환
const s = 6
const ans = s.toString(2);
console.log(ans);
- repeat - 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열을 반환
구문: str.repeat(count);
'abc'.repeat(2);
let answer = '';
for(let i = 0; i < 10; i++){
answer += String(i).repeat(1);
}
console.log(answer);
- Math.max, Math.min이 문자열도 취급