
표준 빌트인 객체인 String은 원시 타입인 문자열을 다룰 때 유용한 프로퍼티와 메서드를 제공한다. 이번 포스팅을 통해 String 객체에 대해 제대로 정리하고 넘어가보자.
"), 작은 따옴표('), 백틱을 사용해 생성할 수 있다.let str1 = "Hello";
let str2 = 'World';
let str3 = `Hello ${str2}`; // 템플릿 리터럴
new 연산자와 함께 호출하여 String 인스턴스를 생성할 수 있다.new 연산자와 함께 호출하면 [[StringDate]] 내부 슬롯에 문자열을 할당한 String 래퍼 객체를 생성한다.const strObj = new String();
console.log(strObj); // String { length: 0, [[Prototype]]: String, [[PrimitiveValue]]: "" }
new 연산자와 함께 호출하면 [[StringDate]] 내부 슬롯에 인수로 전달받은 문자열을 할당한 String 래퍼 객체를 생성한다.const strObj = new String('Sean')
console.log(strObj); // String { 0: "S", 1: "e", 2: "a", 3: "n", , length: 4, [[PrimitiveValue]]: "Sean" }
length 프로퍼티와 인덱스를 나타내는 숫자 형식의 문자를 프로퍼티 키로, 각 문자를 프로퍼티 값으로 갖는 유사 배열 객체이면서 이터러블(순회 가능한)이다.const strObj = new String('Sean')
console.log(strObj[0]); // 'S'
strObj[0] = 'L';
console.log(strObj); // 'Sean'
new 연산자를 사용하지 않고 String 생성자 함수를 호출하면 String 인스턴스가 아닌 문자열을 반환한다.String(1); // "1"
String(NaN); // "NaN"
String(true); // "true"
length 프로퍼티는 문자열의 문자 개수를 반환한다.let str = "Sean";
console.log(str.length); // 4
const strObj = new String('Sean')
console.log(Object.getOwnPropertyDescriptors(strObj));
/* String 래퍼 객체는 읽기 전용 객체이므로 writable 프로퍼티 어트리뷰트 값이 false다. */
{
'0': {value: 'S', writable: false, enumerable: true, configurable: false}
'1': {value: 'e', writable: false, enumerable: true, configurable: false}
'2': {value: 'a', writable: false, enumerable: true, configurable: false}
'3': {value: 'n', writable: false, enumerable: true, configurable: false}
length: {value: 4, writable: false, enumerable: false, configurable: false}
[[Prototype]]: Object
}
let str = "Hello World";
console.log(str.indexOf("World")); // 6
str.indexOf('l', 3); // 3
let str = "Hello World World";
console.log(str.lastIndexOf("World")); // 12
const str = 'Hello world';
str.search(/o/); // 4
str.search(/x/); // -1
let str = "Hello World";
console.log(str.includes("Hello")); // true
let str = "Hello World";
console.log(str.startsWith("Hello")); // true
let str = "Hello World";
console.log(str.endsWith("World")); // true
let str = "Hello";
console.log(str.charAt(1)); // "e"
slice와 유사하지만 음수 인덱스를 지원하지 않는다.let str = "Hello World";
console.log(str.substring(0, 5)); // "Hello"
※
subStr(start, length)는 자바스크립트 표준에서 더 이상 권장되지 않는 비표준 메서드 이므로, 호환성 문제를 피하기 위해subString이나slice를 사용하는 것이 좋다.
subString 메서드와 달리 음수인 인수를 전달할 수 있다.let str = "Hello World";
console.log(str.substring(-5)); // "Hello World"
console.log(str.slice(-5)); // "World"
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
let str = "Hello World";
console.log(str.toLowerCase()); // "hello world"
let str = " Hello World ";
console.log(str.trim()); // "Hello World"
let str = "Hello";
console.log(str.repeat(3)); // "HelloHelloHello"
let str = "Hello World";
console.log(str.replace("World", "JavaScript")); // "Hello JavaScript"
let str = "Hello World World";
console.log(str.replaceAll("World", "JavaScript")); // "Hello JavaScript JavaScript"
let str = "Hello World";
console.log(str.split(" ")); // ["Hello", "World"]
문자열에서 특정 문자를 표시하거나 줄 바꿈 등을 할 때 이스케이프 문자를 사용한다.
\n: 줄 바꿈\t: 탭\': 작은 따옴표\": 큰 따옴표\\: 백슬래시