32장 String
1. String 생성자 함수
- 표준 빌트인 객체인 String은 생성자 함수 객체
- 생성자 함수에 인수를 전달하지 않고 new 연산자와 함께 호출 시, [[StringData]] 내부 슬롯에 빈 문자열을 할당한 String 래퍼 객체를 생성
const strObj = new String();
console.log(strObj);
- 생성자 함수에 인수를 전달하고 new 연산자와 함께 호출 시, [[StringData]] 내부 슬롯에 문자열을 할당한 String 래퍼 객체를 생성
const strObj = new String('Lee');
console.log(strObj);
console.log(strObj[0]);
strObj[0] = 'S';
console.log(strObj);
- 문자열이 아닌 값을 전달하면, 인수를 문자열로 강제 변환
let strObj = new String(123);
console.log(strObj);
strObj = new String(null);
console.log(strObj);
- new 연산자를 사용하지 않고 String 생성자 함수를 호출하면, 인스턴스가 아닌 문자열을 반환
String(1);
String(NaN);
String(Infinity);
String(true);
String(false);
2. length 프로퍼티
'Hello'.length;
'안녕하세요!'.length;
3. String 메서드
- String 객체는 원본을 직접 변경하는 메서드는 존재하지 않고🤗 언제나 새로운 문자열을 반환한다.
3.1 String.prototype.indexOf
- 호출한 문자열에서 인수로 전달받은 문자열을 검색하여 첫 번째 인덱스를 반환.
- 못찾으면 -1 반환
const str = 'Hello World';
str.indexOf('d');
str.indexOf('x');
str.indexOf('l', 4);
3.2 String.prototype.search
- 인수로 정규표현식을 전달받아서 일치하는 문자열의 인덱스를 반환
- 못찾으면 -1 반환
const str = 'Hello World';
str.search(/o/);
str.search(/x/);
3.3 String.prototype.includes
- 인수로 전달받은 문자열이 포함되어 있는지 확인해 boolean 반환
const str = 'Hello world';
str.includes('Hello');
str.includes('');
str.includes('x');
str.includes();
str.includes('l', 3);
str.includes('H', 3);
3.4 String.prototype.startsWith
- ES6 도입
- 대상 문자열이 인수로 전달받은 문자열로 시작하는지 확인해서 boolean 반환
const str = 'Hello world';
str.startsWith('He');
str.startsWith('x');
str.startsWith(' ', 5);
3.5 String.prototype.endsWith
- ES6 도입
- 대상 문자열이 인수로 전달받은 문자열로 끝나는지 확인해서 boolean 반환
const str = 'Hello world';
str.endsWith('ld');
str.endsWith('x');
str.endsWith('lo', 5);
3.6 String.prototype.charAt
- 대상 문자열에서 인수로 전달받은 인덱스에 위치한 문자를 검색해 반환
- 인덱스가 문자열 길이보다 크면 빈 문자열 반환
const str = 'Hello';
for(let i = 0; i < str.length; i++) {
console.log(str.charAt(i));
}
str.charAt(5);
3.7 String.prototype.substring
- 첫번째 인자 ~ 두번째 인자 직전까지의 문자열 리턴
const str = 'Hello World';
str.substring(1, 4);
- indexOf 메서드와 함께 사용해 특정 문자열을 기준으로 앞뒤에 위치한 substirng을 가져올 수 있다
const str = 'Hello World';
str.substring(0, str.indexOf(' '));
str.substring(str.indexOf(' ') + 1, str.length);
3.8 String.prototype.slice
- substring과 유사하지만, 음수 전달 가능
- 음수 전달하면 가장 뒤에서부터 시작
const str = 'Hello World';
str.slice(0,5);
str.slice(-5);
3.9 String.prototype.toUpperCase
const str = 'Hello World';
str.toUpperCase();
3.10 String.prototype.toLowerCase
const str = 'Hello World';
str.toLowerCase();
3.11 String.prototype.trim
- 대상 문자열 앞뒤에 공백 문자가 있을 경우 이를 제거
const str= ' foo ';
str.trim();
3.12 String.prototype.repeat
- 대상 문자열을 인수로 전달받은 정수만큼 반복한 문자열 반환
- 0이면 빈 문자열, 음수면 RangeError 반생
- 인수 생략 시, 0으로 취급
const str = 'abc';
str.repeat();
str.repeat(2);
3.13 String.prototype.replace
- 대상 문자열에서 첫번째 인수로 전달받은 문자열 또는 정규표현식을 검색하여 두 번째 인수로 전달한 문자열로 치환한다
const str = 'Hello world';
str.replace('world', 'Lee');
const str = 'Hello world world';
str.replace('world', 'Lee');
const str = 'Hello Hello';
str.replace(/hello/gi, 'Lee');
3.14 String.prototype.split
- 대상 문자열에서 첫 번째 인수로 전달한 문자열 또는 정규 표현식을 검색해 문자열을 각기 분리, 배열로 반환
- 인수로 빈 문자열 전달 시, 모든 문자열 분리
- 인수 생략 시, 분리 없이 단일 요소 배열 반환
- 두번째 인수로 배열의 길이 지정 가능
const str= "How are you doing?";
str.split(' ');
str.split(' ', 2);