
- String 객체는 생성자 함수 객체다. 따라서 new 연사자와 함께 호출하여 String인스턴스를 생성할 수 있다.
- String 생성자 함수에 인수를 전달하지 않고 ne [[StringData]] 내부 슬롯에 빈 문자열 할당한 String 래퍼 객체를 생성한다.w 연산자와 함께 호출하면
- 배열과 유사하게 인덱스를 사용하여 각 문자에 접근할 수 있다.
- 문자열은 원시 값이므로 변경할 수 없다.
- String 생성자 함수의 인수로 문자열이 아닌 값을 전달하면 인수를 문자열로 강제 변환한 후, [[StringData]] 내부 슬롯에 변환된 문자열을 할당한 String 래퍼 객체를 생성한다.
let strObj = new String(123);
console.log(strObj) -> "123"
- “명시적 타입 변환” new 연산자를 사용하지 않고 String 생성자 함수를 호출하면 String 인스턴스가 아닌 문자열을 반환한다. 이를 이용하여 명시적으로 타입을 변환하기도 한다.
- length → 문자열의 문자 개수를 반환한다.
- String 래퍼 객체는 배열과 마찬가지로 length 프로퍼티를 갖는다. 그리고 인덱스를 나타내는 숫자를 프로퍼티 키로, 각 문자를 프로퍼티 값으로 가지므로 String 래퍼 객체는 유사 배열 객체다.
- String 객체의 메서드는 언제나 새로운 문자열을 반환한다. 문자열은 변경 불가능한 원시 값이기 때문에 String 래퍼 객체도 읽기 전용 객체로 제공된다.
- indexOf 메서드는 대상 문자열(메서드를 호출한 문자열)에서 인수로 전달받은 문자열을 검색하여 첫 번째 인덱스를 반환한다. 검색에 실패하면 -1을 반환한다.
const str = 'hello loh'
console.log(str.indexOf('h'));
console.log(str.indexOf('h', 2));
if (str.indexOf('hello') !== -1) {
console.log(str);
}
- includes 메서드는 대상 문자열에 인수로 전달받은 문자열이 포함되어 있는지 확인하여 그 결과를 true또는 false로 반환한다.
- search 메서드는 대상 문자열에서 인수로 전달받은 정규 표현식과 매치하는 문자열을 검색하여 일치하는 문자열의 인덱스를 반환한다. 검색에 실패하면 -1을 반환한다.
- startsWith메소드는 대상 문자열이 인수로 전달받은 문자열로 시작하는지 확인하여 그 결과를 true 또는 false로 반환한다.
- enssWith 메서드는 대상 문자열이 인수로 전달받은 문자열로 끝나는지 확인하여 그 결과를 true 또는 false로 반환한다.
- charAt 메서드는 대상 문자열에서 인수로 전달받은 인덱스에 위치한 문자를 검색하여 반환한다.
const str = 'HELLO';
for (let i = 0; i < str.length; i++) {
console.log(str.charAt(i));
}
- substring 메서드는 대상 문자열에서 첫 번째 인수로 전달받은 인덱스에 위치하는 문자부터 두 번째 인수로 전달받은 인덱스에 위치하는 문자의 바로 이전 문자까지의 부분 문자열을 반환한다. 두번째 인수는 생략할 수도 있다.
const str = "hello hello"
const aaa = str.substring(1, 4);
console.log(aaa);
------------------------------------------------------
const str = "Hello World";
const aaa = str.substring(0, str.indexOf(' '));
const bbb = str.substring(str.indexOf(' ') + 1, str.length)
console.log(aaa);
console.log(bbb);
- slice 메서드는 substring 메서드와 동일하게 동작한다. 단, slice메서드에는 음수인 인수를 전달할 수 있다. 음수인 인수를 전달하면 대상 문자열의 가장 뒤에서부터 시작하여 문자열을 잘라내어 반환한다.
const str = 'hello wolrd';
const aaa = str.slice(3, 5)
console.log(aaa);
- toUpperCase 메서드는 대상 문자열을 모두 대문자로 변경한 문자열을 반환한다.
const str = 'hello wolrd';
console.log(str.toUpperCase());
- toLowerCase 메서드는 대상 문자열을 모두 소문자로 변경한 문자열을 반환한다.
const str = 'hello WOLRD';
console.log(str.toLowerCase());
- trim 메서드는 대상 문자열 앞뒤에 공백 문자가 있을 경우 이를 제거한 문자열을 반환한다.
const str = ' hello '
const aaa = str.trim();
console.log(aaa);
const str = ' hello d'
const aaa = str.trimStart();
console.log(aaa);
- replace 메서드에 정규 표현식을 인수로 전달하여 공백 문자를 제거할 수도 있다.
const str = ' hel lo '
const aaa = str.replace(/\s/g, '');
console.log(aaa);
const str = ' hel '
const aaa = str.replace(/^\s+/g, '');
console.log(aaa);
const str = ' hel '
const aaa = str.replace(/\s+$/g, '');
console.log(aaa);' hel'
- repeat 메서드는 대상 문자열을 인수로 전달받은 정수만큼 반복해 연결한 새로운 문자열을 반환한다. 인수로 전달받은 정수가 0이면 빈 문자열을 반환하고, 음수이면 RangeError를 발생시킨다. 인수를 생략하면 기본값이 0이 설정된다
const str = 'abc';
console.log(str.repeat());
console.log(str.repeat(0));
console.log(str.repeat(1));
console.log(str.repeat(2));
console.log(str.repeat(2.5));
console.log(str.repeat(-1));
- replace 메서드는 대상 문자열에서 첫 번째 인수로 전달받은 문자열 또는 정규표현식을 검색하여 두 번째 인수로 전달한 문자열로 치환한 문자열을 반환한다.
const str = 'hello world'
const aaa = str.replace('hello', 'bye');
console.log(aaa);
const str = 'Hello Hello';
const aaa = str.replace(/hello/gi, 'Lee');
console.log(aaa);
- split 메서드는 대상 문자열에서 첫 번째 인수로 전달한 문자열 또는 정규 표현식을 검색하여여 문자열을 구별한 후 분리된 각 문자열로 이루어진 배열을 반환한다. 인수로로 빈 문자열을 전달하면 각 문자를 모두 분리하고, 인수를 생략하면 대상 문자열 전체를 단일 요소로 하는 배열을 반환한다.
const str = 'How are you doing?';
console.log(str.split(' '));
console.log(str.split(/\s/));
console.log(str.split(''));
'H', 'o', 'w', ' ', 'a',
'r', 'e', ' ', 'y', 'o',
'u', ' ', 'd', 'o', 'i',
'n', 'g', '?'
]
console.log(str.split());
const str = 'How are you doing?';
const aaa = str.split('').reverse().join('')
console.log(aaa);