const strObj = new String();
console.log(strObj); // String {length: 0, [[PrimitiveValue]]: ""}
const strObj = new String('Lee');
console.log(strObj);
// String {0: "L", 1: "e", 2: "e", length: 3, [[PrimitiveValue]]: "Lee"}
console.log(strObj[0]); // L
// ๋ฌธ์์ด์ ์์๊ฐ์ด๋ฏ๋ก ๋ณ๊ฒฝํ ์ ์๋ค. ์ด๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
strObj[0] = 'S';
console.log(strObj); // 'Lee'
let strObj = new String(123);
console.log(strObj);
// String {0: "1", 1: "2", 2: "3", length: 3, [[PrimitiveValue]]: "123"}
strObj = new String(null);
console.log(strObj);
// String {0: "n", 1: "u", 2: "l", : "l", length: 4, [[PrimitiveValue]]: "null"}
// ์ซ์ ํ์
=> ๋ฌธ์์ด ํ์
String(1); // -> "1"
String(NaN); // -> "NaN"
String(Infinity); // -> "Infinity"
// ๋ถ๋ฆฌ์ธ ํ์
=> ๋ฌธ์์ด ํ์
String(true); // -> "true"
String(false); // -> "false"
'Hello'.length; // -> 5
'์๋
ํ์ธ์!'.length; // -> 6
const strObj = new String('Lee');
console.log(Object.getOwnPropertyDescriptors(strObj));
/* String ๋ํผ ๊ฐ์ฒด๋ ์ฝ๊ธฐ ์ ์ฉ ๊ฐ์ฒด๋ค. ์ฆ, writable ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด false๋ค.
{
'0': { value: 'L', writable: false, enumerable: true, configurable: false },
'1': { value: 'e', writable: false, enumerable: true, configurable: false },
'2': { value: 'e', writable: false, enumerable: true, configurable: false },
length: { value: 3, writable: false, enumerable: false, configurable: false }
}
*/
const str = 'Hello World';
// ๋ฌธ์์ด str์์ 'l'์ ๊ฒ์ํ์ฌ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค.
str.indexOf('l'); // -> 2
// ๋ฌธ์์ด str์์ 'or'์ ๊ฒ์ํ์ฌ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค.
str.indexOf('or'); // -> 7
// ๋ฌธ์์ด str์์ 'x'๋ฅผ ๊ฒ์ํ์ฌ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค. ๊ฒ์์ ์คํจํ๋ฉด -1์ ๋ฐํํ๋ค.
str.indexOf('x'); // -> -1
// ๋ฌธ์์ด str์ ์ธ๋ฑ์ค 3๋ถํฐ 'l'์ ๊ฒ์ํ์ฌ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค.
str.indexOf('l', 3); // -> 3
if (str.indexOf('Hello') !== -1) {
// ๋ฌธ์์ด str์ 'Hello'๊ฐ ํฌํจ๋์ด ์๋ ๊ฒฝ์ฐ์ ์ฒ๋ฆฌํ ๋ด์ฉ
}
if (str.includes('Hello')) {
// ๋ฌธ์์ด str์ 'Hello'๊ฐ ํฌํจ๋์ด ์๋ ๊ฒฝ์ฐ์ ์ฒ๋ฆฌํ ๋ด์ฉ
}
const str = 'Hello world';
// ๋ฌธ์์ด str์์ ์ ๊ท ํํ์๊ณผ ๋งค์นํ๋ ๋ฌธ์์ด์ ๊ฒ์ํ์ฌ ์ผ์นํ๋ ๋ฌธ์์ด์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ๋ค.
str.search(/o/); // -> 4
str.search(/x/); // -> -1
const str = 'Hello world';
str.includes('Hello'); // -> true
str.includes(''); // -> true
str.includes('x'); // -> false
str.includes(); // -> false
const str = 'Hello world';
// ๋ฌธ์์ด str์ ์ธ๋ฑ์ค 3๋ถํฐ 'l'์ด ํฌํจ๋์ด ์๋์ง ํ์ธ
str.includes('l', 3); // -> true
str.includes('H', 3); // -> false
const str = 'Hello world';
// ๋ฌธ์์ด str์ด 'He'๋ก ์์ํ๋์ง ํ์ธ
str.startsWith('He'); // -> true
// ๋ฌธ์์ด str์ด 'x'๋ก ์์ํ๋์ง ํ์ธ
str.startsWith('x'); // -> false
// ๋ฌธ์์ด str์ ์ธ๋ฑ์ค 5๋ถํฐ ์์ํ๋ ๋ฌธ์์ด์ด ' '๋ก ์์ํ๋์ง ํ์ธ
str.startsWith(' ', 5); // -> true
const str = 'Hello world';
// ๋ฌธ์์ด str์ด 'ld'๋ก ๋๋๋์ง ํ์ธ
str.endsWith('ld'); // -> true
// ๋ฌธ์์ด str์ด 'x'๋ก ๋๋๋์ง ํ์ธ
str.endsWith('x'); // -> false
// ๋ฌธ์์ด str์ ์ฒ์๋ถํฐ 5์๋ฆฌ๊น์ง('Hello')๊ฐ 'lo'๋ก ๋๋๋์ง ํ์ธ
str.endsWith('lo', 5); // -> true
const str = 'Hello';
for (let i = 0; i < str.length; i++) {
console.log(str.charAt(i)); // H e l l o
}
// ์ธ๋ฑ์ค๊ฐ ๋ฌธ์์ด์ ๋ฒ์(0 ~ str.length-1)๋ฅผ ๋ฒ์ด๋ ๊ฒฝ์ฐ ๋น๋ฌธ์์ด์ ๋ฐํํ๋ค.
str.charAt(5); // -> ''
const str = 'Hello World';
// ์ธ๋ฑ์ค 1๋ถํฐ ์ธ๋ฑ์ค 4 ์ด์ ๊น์ง์ ๋ถ๋ถ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
str.substring(1, 4); // -> ell
const str = 'Hello World';
// ์ธ๋ฑ์ค 1๋ถํฐ ๋ง์ง๋ง ๋ฌธ์๊น์ง ๋ถ๋ถ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
str.substring(1); // -> 'ello World'
const str = 'Hello World'; // str.length == 11
// ์ฒซ ๋ฒ์งธ ์ธ์ > ๋ ๋ฒ์งธ ์ธ์์ธ ๊ฒฝ์ฐ ๋ ์ธ์๋ ๊ตํ๋๋ค.
str.substring(4, 1); // -> 'ell'
// ์ธ์ < 0 ๋๋ NaN์ธ ๊ฒฝ์ฐ 0์ผ๋ก ์ทจ๊ธ๋๋ค.
str.substring(-2); // -> 'Hello World'
// ์ธ์ > ๋ฌธ์์ด์ ๊ธธ์ด(str.length)์ธ ๊ฒฝ์ฐ ์ธ์๋ ๋ฌธ์์ด์ ๊ธธ์ด(str.length)์ผ๋ก ์ทจ๊ธ๋๋ค.
str.substring(1, 100); // -> 'ello World'
str.substring(20); // -> ''
const str = 'Hello World';
// ์คํ์ด์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์๋ ๋ถ๋ถ ๋ฌธ์์ด ์ทจ๋
str.substring(0, str.indexOf(' ')); // -> 'Hello'
// ์คํ์ด์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค์ ์๋ ๋ถ๋ถ ๋ฌธ์์ด ์ทจ๋
str.substring(str.indexOf(' ') + 1, str.length); // -> 'World'
const str = 'hello world';
// substring๊ณผ slice ๋ฉ์๋๋ ๋์ผํ๊ฒ ๋์ํ๋ค.
// 0๋ฒ์งธ๋ถํฐ 5๋ฒ์งธ ์ด์ ๋ฌธ์๊น์ง ์๋ผ๋ด์ด ๋ฐํ
str.substring(0, 5); // -> 'hello'
str.slice(0, 5); // -> 'hello'
// ์ธ๋ฑ์ค๊ฐ 2์ธ ๋ฌธ์๋ถํฐ ๋ง์ง๋ง ๋ฌธ์๊น์ง ์๋ผ๋ด์ด ๋ฐํ
str.substring(2); // -> 'llo world'
str.slice(2); // -> 'llo world'
// ์ธ์ < 0 ๋๋ NaN์ธ ๊ฒฝ์ฐ 0์ผ๋ก ์ทจ๊ธ๋๋ค.
str.substring(-5); // -> 'hello world'
// slice ๋ฉ์๋๋ ์์์ธ ์ธ์๋ฅผ ์ ๋ฌํ ์ ์๋ค. ๋ค์์ 5์๋ฆฌ๋ฅผ ์๋ผ๋ด์ด ๋ฐํํ๋ค.
str.slice(-5); // โถ 'world'
const str = 'Hello World!';
str.toUpperCase(); // -> 'HELLO WORLD!'
const str = 'Hello World!';
str.toLowerCase(); // -> 'hello world!'
const str = ' foo ';
str.trim(); // -> 'foo'
const str = ' foo ';
// String.prototype.{trimStart,trimEnd} : Proposal stage 4
str.trimStart(); // -> 'foo '
str.trimEnd(); // -> ' foo'
const str = ' foo ';
// ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์ ๊ท ํํ์์ ๋งค์นํ๋ ๋ฌธ์์ด์ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๋ก ์นํํ๋ค.
str.replace(/\s/g, ''); // -> 'foo'
str.replace(/^\s+/g, ''); // -> 'foo '
str.replace(/\s+$/g, ''); // -> ' foo'
const str = 'abc';
str.repeat(); // -> ''
str.repeat(0); // -> ''
str.repeat(1); // -> 'abc'
str.repeat(2); // -> 'abcabc'
str.repeat(2.5); // -> 'abcabc' (2.5 โ 2)
str.repeat(-1); // -> RangeError: Invalid count value
const str = 'Hello world';
// str์์ ์ฒซ ๋ฒ์งธ ์ธ์ 'world'๋ฅผ ๊ฒ์ํ์ฌ ๋ ๋ฒ์งธ ์ธ์ 'Lee'๋ก ์นํํ๋ค.
str.replace('world', 'Lee'); // -> 'Hello Lee'
const str = 'Hello world world';
str.replace('world', 'Lee'); // -> 'Hello Lee world'
const str = 'Hello world';
// ํน์ํ ๊ต์ฒด ํจํด์ ์ฌ์ฉํ ์ ์๋ค. ($& => ๊ฒ์๋ ๋ฌธ์์ด)
str.replace('world', '<strong>$&</strong>');
const str = 'Hello Hello';
// 'hello'๋ฅผ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ์ ์ญ ๊ฒ์ํ๋ค.
str.replace(/hello/gi, 'Lee'); // -> 'Lee Lee'
// ์นด๋ฉ ์ผ์ด์ค๋ฅผ ์ค๋ค์ดํฌ ์ผ์ด์ค๋ก ๋ณํํ๋ ํจ์
function camelToSnake(camelCase) {
// /.[A-Z]/g๋ ์์์ ํ ๋ฌธ์์ ๋๋ฌธ์๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ ๋งค์นํ๋ค.
// ์นํ ํจ์์ ์ธ์๋ก ๋งค์น ๊ฒฐ๊ณผ๊ฐ ์ ๋ฌ๋๊ณ , ์นํ ํจ์๊ฐ ๋ฐํํ ๊ฒฐ๊ณผ์ ๋งค์น ๊ฒฐ๊ณผ๋ฅผ ์นํํ๋ค.
return camelCase.replace(/.[A-Z]/g, match => {
console.log(match); // 'oW'
return match[0] + '_' + match[1].toLowerCase();
});
}
const camelCase = 'helloWorld';
camelToSnake(camelCase); // -> 'hello_world'
// ์ค๋ค์ดํฌ ์ผ์ด์ค๋ฅผ ์นด๋ฉ ์ผ์ด์ค๋ก ๋ณํํ๋ ํจ์
function snakeToCamel(snakeCase) {
// /_[a-z]/g๋ _์ ์๋ฌธ์๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ ๋งค์นํ๋ค.
// ์นํ ํจ์์ ์ธ์๋ก ๋งค์น ๊ฒฐ๊ณผ๊ฐ ์ ๋ฌ๋๊ณ , ์นํ ํจ์๊ฐ ๋ฐํํ ๊ฒฐ๊ณผ์ ๋งค์น ๊ฒฐ๊ณผ๋ฅผ ์นํํ๋ค.
return snakeCase.replace(/_[a-z]/g, match => {
console.log(match); // '_w'
return match[1].toUpperCase();
});
}
const snakeCase = 'hello_world';
snakeToCamel(snakeCase); // -> 'helloWorld'
const str = 'How are you doing?';
// ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ(๋จ์ด๋ก ๊ตฌ๋ถ)ํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํํ๋ค.
str.split(' '); // -> ["How", "are", "you", "doing?"]
// \s๋ ์ฌ๋ฌ ๊ฐ์ง ๊ณต๋ฐฑ ๋ฌธ์(์คํ์ด์ค, ํญ ๋ฑ)๋ฅผ ์๋ฏธํ๋ค. ์ฆ, [\t\r\n\v\f]์ ๊ฐ์ ์๋ฏธ๋ค.
str.split(/\s/); // -> ["How", "are", "you", "doing?"]
// ์ธ์๋ก ๋น ๋ฌธ์์ด์ ์ ๋ฌํ๋ฉด ๊ฐ ๋ฌธ์๋ฅผ ๋ชจ๋ ๋ถ๋ฆฌํ๋ค.
str.split(''); // -> ["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", " ", "d", "o", "i", "n", "g", "?"]
// ์ธ์๋ฅผ ์๋ตํ๋ฉด ๋์ ๋ฌธ์์ด ์ ์ฒด๋ฅผ ๋จ์ผ ์์๋ก ํ๋ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
str.split(); // -> ["How are you doing?"]
// ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํํ๋ค. ๋จ, ๋ฐฐ์ด์ ๊ธธ์ด๋ 3์ด๋ค
str.split(' ', 3); // -> ["How", "are", "you"]
// ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ฌธ์์ด์ ์ญ์์ผ๋ก ๋ค์ง๋๋ค.
function reverseString(str) {
return str.split('').reverse().join('');
}
reverseString('Hello world!'); // -> '!dlrow olleH'