Skip to content

Latest commit

ย 

History

History
424 lines (304 loc) ยท 9.6 KB

File metadata and controls

424 lines (304 loc) ยท 9.6 KB

32-01

const strObj = new String();
console.log(strObj); // String {length: 0, [[PrimitiveValue]]: ""}

32-02

const strObj = new String('Lee');
console.log(strObj);
// String {0: "L", 1: "e", 2: "e", length: 3, [[PrimitiveValue]]: "Lee"}

32-03

console.log(strObj[0]); // L

32-04

// ๋ฌธ์ž์—ด์€ ์›์‹œ๊ฐ’์ด๋ฏ€๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
strObj[0] = 'S';
console.log(strObj); // 'Lee'

32-05

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"}

32-06

// ์ˆซ์ž ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
String(1);        // -> "1"
String(NaN);      // -> "NaN"
String(Infinity); // -> "Infinity"

// ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
String(true);  // -> "true"
String(false); // -> "false"

32-07

'Hello'.length;    // -> 5
'์•ˆ๋…•ํ•˜์„ธ์š”!'.length; // -> 6

32-08

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 }
}
*/

32-09

const str = 'Hello World';

// ๋ฌธ์ž์—ด str์—์„œ 'l'์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.indexOf('l'); // -> 2


// ๋ฌธ์ž์—ด str์—์„œ 'or'์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.indexOf('or'); // -> 7

// ๋ฌธ์ž์—ด str์—์„œ 'x'๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฒ€์ƒ‰์— ์‹คํŒจํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.indexOf('x'); // -> -1

32-10

// ๋ฌธ์ž์—ด str์˜ ์ธ๋ฑ์Šค 3๋ถ€ํ„ฐ 'l'์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.indexOf('l', 3); // -> 3

32-11

if (str.indexOf('Hello') !== -1) {
  // ๋ฌธ์ž์—ด str์— 'Hello'๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ์ฒ˜๋ฆฌํ•  ๋‚ด์šฉ
}

32-12

if (str.includes('Hello')) {
  // ๋ฌธ์ž์—ด str์— 'Hello'๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์— ์ฒ˜๋ฆฌํ•  ๋‚ด์šฉ
}

32-13

const str = 'Hello world';

// ๋ฌธ์ž์—ด str์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ๋งค์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.search(/o/); // -> 4
str.search(/x/); // -> -1

32-14

const str = 'Hello world';

str.includes('Hello'); // -> true
str.includes('');      // -> true
str.includes('x');     // -> false
str.includes();        // -> false

32-15

const str = 'Hello world';

// ๋ฌธ์ž์—ด str์˜ ์ธ๋ฑ์Šค 3๋ถ€ํ„ฐ 'l'์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
str.includes('l', 3); // -> true
str.includes('H', 3); // -> false

32-16

const str = 'Hello world';

// ๋ฌธ์ž์—ด str์ด 'He'๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ
str.startsWith('He'); // -> true
// ๋ฌธ์ž์—ด str์ด 'x'๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ
str.startsWith('x'); // -> false

32-17

// ๋ฌธ์ž์—ด str์˜ ์ธ๋ฑ์Šค 5๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์ด ' '๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ
str.startsWith(' ', 5); // -> true

32-18

const str = 'Hello world';

// ๋ฌธ์ž์—ด str์ด 'ld'๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธ
str.endsWith('ld'); // -> true
// ๋ฌธ์ž์—ด str์ด 'x'๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธ
str.endsWith('x'); // -> false

32-19

// ๋ฌธ์ž์—ด str์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ 5์ž๋ฆฌ๊นŒ์ง€('Hello')๊ฐ€ 'lo'๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธ
str.endsWith('lo', 5); // -> true

32-20

const str = 'Hello';

for (let i = 0; i < str.length; i++) {
  console.log(str.charAt(i)); // H e l l o
}

32-21

// ์ธ๋ฑ์Šค๊ฐ€ ๋ฌธ์ž์—ด์˜ ๋ฒ”์œ„(0 ~ str.length-1)๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ ๋นˆ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.charAt(5); // -> ''

32-22

const str = 'Hello World';

// ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ ์ธ๋ฑ์Šค 4 ์ด์ „๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.substring(1, 4); // -> ell

32-23

const str = 'Hello World';

// ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊นŒ์ง€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
str.substring(1); // -> 'ello World'

32-24

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); // -> ''

32-25

const str = 'Hello World';

// ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์— ์žˆ๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ทจ๋“
str.substring(0, str.indexOf(' ')); // -> 'Hello'

// ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋’ค์— ์žˆ๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ทจ๋“
str.substring(str.indexOf(' ') + 1, str.length); // -> 'World'

32-26

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'

32-27

const str = 'Hello World!';

str.toUpperCase(); // -> 'HELLO WORLD!'

32-28

const str = 'Hello World!';

str.toLowerCase(); // -> 'hello world!'

32-29

const str = '   foo  ';

str.trim(); // -> 'foo'

32-30

const str = '   foo  ';

// String.prototype.{trimStart,trimEnd} : Proposal stage 4
str.trimStart(); // -> 'foo  '
str.trimEnd();   // -> '   foo'

32-31

const str = '   foo  ';

// ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋งค์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•œ๋‹ค.
str.replace(/\s/g, '');   // -> 'foo'
str.replace(/^\s+/g, ''); // -> 'foo  '
str.replace(/\s+$/g, ''); // -> '   foo'

32-32

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

32-33

const str = 'Hello world';

// str์—์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜ 'world'๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜ 'Lee'๋กœ ์น˜ํ™˜ํ•œ๋‹ค.
str.replace('world', 'Lee'); // -> 'Hello Lee'

32-34

const str = 'Hello world world';

str.replace('world', 'Lee'); // -> 'Hello Lee world'

32-35

const str = 'Hello world';

// ํŠน์ˆ˜ํ•œ ๊ต์ฒด ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ($& => ๊ฒ€์ƒ‰๋œ ๋ฌธ์ž์—ด)
str.replace('world', '<strong>$&</strong>');

32-36

const str = 'Hello Hello';

// 'hello'๋ฅผ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๊ณ  ์ „์—ญ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
str.replace(/hello/gi, 'Lee'); // -> 'Lee Lee'

32-37

// ์นด๋ฉœ ์ผ€์ด์Šค๋ฅผ ์Šค๋„ค์ดํฌ ์ผ€์ด์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
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'

32-38

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?"]

32-39

// ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‹จ, ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 3์ด๋‹ค
str.split(' ', 3); // -> ["How", "are", "you"]

32-40

// ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์—ญ์ˆœ์œผ๋กœ ๋’ค์ง‘๋Š”๋‹ค.
function reverseString(str) {
  return str.split('').reverse().join('');
}

reverseString('Hello world!'); // -> '!dlrow olleH'