Chapter 05: ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ (Additional string methods)

๐Ÿ’ก ๋„์„œ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•ต์‹ฌ ๊ฐ€์ด๋“œ๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

5.1 ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ


indexOf()

Gets the position of the first occurrence of the specified value in a string.

๋ฌธ์ž์—ด์—์„œ ์ง€์ •๋œ ๊ฐ’์ด ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜

const str = "this is a short sentence";
str.indexOf("short");
// Output: 10

ํ™œ์šฉ ์˜ˆ์‹œ

// ํŒŒ์ผ์˜ ํ™•์žฅ์ž ํ˜น์€ ์ •๊ทœ์‹ ๊ฒ€์‚ฌ ๋“ฑ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.
if ( file.indexOf("xlsx") > -1  ) {
    // ...
}

slice()

Pulls a specified part of a string as a new string.

๋ฌธ์ž์—ด์˜ ์ง€์ •๋œ ๋ถ€๋ถ„์„ ์ƒˆ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ (์›๋ณธ์€ ๋ณ€ํ•˜์ง€ ์•Š์Œ)

const str = "pizza, orange, cereals"
str.slice(0, 5);
// Output: "pizza"

toUpperCase()

Turns all characters of a string to uppercase.

๋ฌธ์ž์—ด ๋‚ด์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พผ๋‹ค.

const str = "i ate an apple"
str.toUpperCase()
// Output: "I ATE AN APPLE"

toLowerCase()

Turns all characters of a string to lowercase.

๋ฌธ์ž์—ด ๋‚ด์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ๋‹ค.

const str = "I ATE AN APPLE"
str.toLowerCase()
// Output: "i ate an apple"

5.2 ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ


๋‹ค์Œ ๋„ค ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๋Š” ES6์—์„œ ์ƒˆ๋กœ ๋„์ž…๋œ ๋ฉ”์„œ๋“œ์ด๋‹ค

  • startsWith()
  • endsWith()
  • includes()
  • repeat()

startsWith()

This new method will check if the string starts with the value we pass in:

๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ boolean ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const code = "ABCDEFG";

code.startsWith("ABB");
// false
code.startsWith("abc");
// false, startsWith is case sensitive
code.startsWith("ABC");
// true

๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฒ€์‚ฌ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ์‹œ์ž‘์ ์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const code = "ABCDEFGHI"

code.startsWith("DEF",3);
// true, 3๊ฐœ ๋ฌธ์ž๋ฅผ ์ง€๋‚˜์„œ ๊ณ„์‚ฐ

endsWith()

Similarly to startsWith(), this new method will check if the string ends with the value we pass in:

startsWith() ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฌธ์ž์—ด์ด ์šฐ๋ฆฌ๊ฐ€ ์ „๋‹ฌํ•œ ๊ฐ’์œผ๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

const code = "ABCDEF";

code.endsWith("DDD");
// false
code.endsWith("def");
// false, endsWith is case sensitive
code.endsWith("DEF");
// true

๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด์˜ ๋ฒ”์œ„๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const code = "ABCDEFGHI"

code.endsWith("EF", 6);
// true, ์ฒซ 6๊ฐœ ๋ฌธ์ž์ธ ABCDEF๋งŒ์„ ๊ณ ๋ ค

includes()

This method will check if our string includes the value we pass in.

์šฐ๋ฆฌ๊ฐ€ ์ „๋‹ฌํ•œ ๊ฐ’์ด ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

const code = "ABCDEF"

code.includes("ABB");
// false
code.includes("abc");
// false, includes is case sensitive
code.includes("CDE");
// true

repeat()

This new method will take an argument that specifies the number of times it needs to repeat the string .

ํšŸ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ ๋ฌธ์ž์—ด์„ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

let hello = "Hi";
console.log(hello.repeat(10));
// "HiHiHiHiHiHiHiHiHiHi"