String 생성자 함수 - new String(value)
전달된 인자는 모두 문자열로 변환
let strObj = new String('Lee');
console.log(strObj); // [String: 'Lee']
new 연산자를 사용하지 않고 String 생성자 함수를 호출하면 String 객체가 아닌 문자열 리터럴을 반환
var x = String('Lee');
console.log(typeof x, x); // string Lee
const str = 'Lee';
const strObj = new String('Lee');
console.log(str == strObj); // true
console.log(str === strObj); // false
console.log(typeof str); // string
console.log(typeof strObj); // object
String.length
문자열 내의 문자 갯수를 반환
String 객체는 length 프로퍼티를 소유하고 있으므로 유사 배열 객체
String Method - String 객체의 모든 메소드는 언제나 새로운 문자열을 반환
문자열은 변경 불가능한 원시 값이기 때문
1. String.charAt(index number) - index에 해당하는 위치의 문자를 반환
지정한 index가 문자열의 범위를 벗어난 경우 빈문자열 반환
String.concat(문자열 1[,문자열 2,...,문자열 N]) - 인수로 전달한 1개 이상의 문자열과 연결하여 새로운 문자열을 반환
concat 메소드를 사용하는 것보다는 +, += 할당 연산자를 사용하는 것이 성능상 유리
String.indexOf(문자열, 시작 index (생략할 경우, 0)) - 문자열을 검색하여 처음 발견된 곳의 index를 반환, 발견하지 못한 경우 -1을 반환
String.lastIndexOf(문자열, 시작 index) - 문자열을 검색하여 마지막으로 발견된 곳의 index를 반환, 발견하지 못한 경우 -1을 반환
2번째 인수가 전달되면 검색 시작 위치를 시작 Index으로 이동하여 역방향으로 검색, 검색 범위는 0 ~ fromIndex
String.replace(기존 문자열, 치환 문자열) - 첫번째 인수로 전달한 문자열을 검색하여 두번째 인수로 전달한 문자열로 대체
원본 문자열은 변경되지 않고 결과가 반영된 새로운 문자열을 반환
검색된 문자열이 여럿 존재할 경우 첫번째로 검색된 문자열만 대체
const str = 'Hello world';
// 특수한 교체 패턴을 사용할 수 있다. ($& => 검색된 문자열)
console.log(str.replace('world', '<strong>$&</strong>')); // Hello <strong>world</strong>
const camelCase = 'helloWorld';
// 두번째 인수로 치환 함수를 전달할 수 있다.
// /.[A-Z]/g => 1문자와 대문자의 조합을 문자열 전체에서 검색한다.
console.log(camelCase.replace(/.[A-Z]/g, function (match) {
// match : oW => match[0] : o, match[1] : W
return match[0] + '_' + match[1].toLowerCase();
})); // hello_world
// /(.)([A-Z])/g => 1문자와 대문자의 조합
// $1 => (.)
// $2 => ([A-Z])
console.log(camelCase.replace(/(.)([A-Z])/g, '$1_$2').toLowerCase()); // hello_world
const str = 'How are you doing?';
// 정규 표현식
console.log(str.split(/\s/)); // [ 'How', 'are', 'you', 'doing?' ]
// 인수가 없는 경우, 대상 문자열 전체를 단일 요소로 하는 배열을 반환한다.
console.log(str.split()); // [ 'How are you doing?' ]
// 각 문자를 모두 분리한다
console.log(str.split('')); // [ 'H','o','w',' ','a','r','e',' ','y','o','u',' ','d','o','i','n','g','?' ]
// 공백으로 구분하여 배열로 반환한다. 단 요소수는 3개까지만 허용한다
console.log(str.split(' ', 3)); // [ 'How', 'are', 'you' ]
const str = 'Hello World'; // str.length == 11
// 인수 < 0 또는 NaN인 경우 : 0으로 취급된다.
console.log(str.substring(-2)); // Hello World
// 인수 > 문자열의 길이 : 인수는 문자열의 길이로 취급된다.
console.log(str.substring(1, 12)); // ello World
console.log(str.substring(20)); // ''
const str = 'hello world';
// 인수 < 0 또는 NaN인 경우 : 0으로 취급된다.
console.log(str.substring(-5)); // 'hello world'
// 뒤에서 5자리를 잘라내어 반환한다.
console.log(str.slice(-5)); // 'world'
const str = ' foo ';
console.log(str.trim()); // 'foo'
// String.prototype.replace
console.log(str.replace(/^\s+/g, '')); // 'foo '
console.log(str.replace(/\s+$/g, '')); // ' foo'
// String.{trimStart,trimEnd} : Proposal stage 3
console.log(str.trimStart()); // 'foo '
console.log(str.trimEnd()); // ' foo'
console.log('abc'.repeat(0)); // ''
console.log('abc'.repeat(2)); // 'abcabc'
console.log('abc'.repeat(2.5)); // 'abcabc' (2.5 → 2)
console.log('abc'.repeat(-1)); // RangeError: Invalid count value
const str = 'hello world';
console.log(str.includes(' ')); // true
console.log(str.includes('')); // true
console.log(str.includes()); // false