Front-end Developer

0%

๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ์™€ ๊ด€๋ จ๋œ ํ•จ์ˆ˜ (substr, substring, slice)

substr

๐Ÿ’ก substr์€ ์ƒˆ๋กœ์šด ์›น์‚ฌ์ดํŠธ์—์„œ๋Š” ๋”์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค. ์›น ํ‘œ์ค€์—์„œ ์ œ๊ฑฐ๋œ ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ๋‹ค๋ฅธ ๋งค์„œ๋“œ๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค.

๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž ์ˆ˜ ๋งŒํผ์˜ ๋ฌธ์ž๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1
2
3
4
5
6
7
const str = 'hello'

console.log(str.substr(1, 2))
// expected output: "el"

console.log(str.substr(2))
// expected output: "llo"

str.substr(start[, length])
str.substr(์‹œ์ž‘์œ„์น˜์˜ ์ธ๋ฑ์Šค, ์ถ”์ถœํ•œ ๋ฌธ์ž์˜ length )

  • start ๊ฐ’์ด ์–‘์ˆ˜์ด๊ณ , ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • start๊ฐ€ ์Œ์ˆ˜์ด๋ฉด ๋ฌธ์ž์—ด ๋์—์„œ start ์ˆซ์ž๋งŒํผ ๋บ€ ๊ณณ์—์„œ ์‹œ์ž‘ํ•œ๋‹ค.
  • start๊ฐ€ ์Œ์ˆ˜์ด๊ณ , ์ ˆ๋Œ€๊ฐ’์ด ๋ฌธ์ž์—ด ์ „์ฒด๋ณด๋‹ค ํฌ๋ฉด ๋ฌธ์ž์—ด์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘
  • length๊ฐ€ 0 ์ด๊ฑฐ๋‚˜ ์Œ์ˆ˜์ด๋ฉด ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • length๊ฐ€ ์ƒ๋žต๋˜์—ˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ์ถ”์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const str = 'helloworld'

str.substr(1, 2)
// 1๋ฒˆ์งธ index์—์„œ 2๊ฐœ ์ถ”์ถœ
// expected output:'el'
str.substr(-3, 2)
// ์ „์ฒด ๋ฌธ์ž์—ด ๊ธธ์ด 10์—์„œ -3๋งŒํผ ๋บ€ ๊ณณ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ 2๊ฐœ ์ถ”์ถœ
// expected output:'rl'
str.substr(-3)
// start๊ฐ€ ์Œ์ˆ˜์ด๋ฏ€๋กœ ์ „์ฒด ๋ฌธ์ž์—ด ๊ธธ๋ฆฌ 10์—์„œ -3๋งŒํผ ๋บ€ ๊ณณ์—์„œ ์‹œ์ž‘,
// length๋Š” ์ƒ๋žต๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ์ถ”์ถœ
// expected output:'rld'
str.substr(1)
// 1๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•˜์—ฌ length๋Š” ์ƒ๋žต๋˜์—ˆ์œผ๋ฏ€๋กœ ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ์ถ”์ถœ
// expected output:'elloworld'
str.substr(-20, 2)
// start๊ฐ€ ์Œ์ˆ˜์ด๊ณ , ์ ˆ๋Œ€๊ฐ’์ด ๋ฌธ์ž์—ด ์ „์ฒด๋ณด๋‹ค ํฌ๋ฉด ๋ฌธ์ž์—ด์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ 2๊ฐœ ์ถ”์ถœ
// expected output:'he'
str.substr(20, 2)
// start๊ฐ€ ์–‘์ˆ˜์ด๋‚˜ ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
// expected output:''

substring

์‹œ์ž‘ ์ธ๋ฑ์Šค๋กœ ๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ „ ๊นŒ์ง€ ๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜

1
2
3
4
5
6
7
const str = 'hello'

console.log(str.substr(1, 3))
// expected output: "el"

console.log(str.substr(2))
// expected output: "llo"

str.substring(indexStart[, indexEnd])
str.substring(์‹œ์ž‘ ์ธ๋ฑ์Šค, ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค(ํฌํ•จํ•˜์ง€ ์•Š์Œ))
return ๊ฐ’: ๊ธฐ์กด๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด

  • ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • indexEnd๊ฐ€ ์ƒ๋žต๋˜์—ˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด ๋๊นŒ์ง€ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•œ๋‹ค.
  • indexStart, indexEnd๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํด ๊ฒฝ์šฐ ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ”๊พผ ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค.
  • 0๋ณด๋‹ค ์ž‘์€ ์ธ์ž ๊ฐ’์„ ๊ฐ€์ง€๋ฉด 0, stringName.length๋ณด๋‹ค ํฐ ์ธ์ž๊ฐ’์„ ๊ฐ€์ง€๋ฉด stringName.length๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
  • NaN ๊ฐ’์€ 0์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const anyString = 'helloworld'

// Displays 'h'
console.log(anyString.substring(0, 1))
//indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํด ๊ฒฝ์šฐ substring(0, 1)์ฒ˜๋Ÿผ ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ”๊พผ๋“ฏ์ด ์ž‘๋™ํ•œ๋‹ค.
console.log(anyString.substring(1, 0))

// Displays 'hello'
console.log(anyString.substring(0, 6))

//Displays 'world'
console.log(anyString.substring(5))

//Displays 'wo'
console.log(anyString.substring(4, 7))
//indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํด ๊ฒฝ์šฐ substring(4, 7)์ฒ˜๋Ÿผ ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ”๊พผ๋“ฏ์ด ์ž‘๋™ํ•œ๋‹ค.
console.log(anyString.substring(7, 4))

// Displays 'helloworld'
console.log(anyString.substring(0, 10))
//stringName.length๋ณด๋‹ค ํฐ ์ธ์ž๊ฐ’์„ ๊ฐ€์ง€๋ฉด stringName.length๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
console.log(anyString.substring(0, 15))

substring()์™€ length ์†์„ฑ์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
// Displays 'orld' the last 4 characters
const anyString = 'helloworld'
const anyString4 = anyString.substring(anyString.length - 4)
console.log(anyString4)

// Displays 'world' the last 5 characters
const anyString = 'helloworld'
const anyString5 = anyString.substring(anyString.length - 5)
console.log(anyString5)

substring ๋ฉ”์„œ๋“œ์™€ length ์†ก์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด anyString.substring(4, 7)๊ณผ ๊ฐ™์ด ์‹œ์ž‘์ธ๋ฑ์Šค์™€ ๋งˆ์ง€๋ง‰์ธ๋ฑ์Šค๋ฅผ ์•Œ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

substring()์™€ substr()์˜ ์ฐจ์ด์ 

substring()์™€ substr()์—๋Š” ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. substring()์€ ์‹œ์ž‘๊ณผ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, substr()์€ ์‹œ์ž‘์ธ๋ฑ์Šค์™€ ๋ฐ˜ํ™˜๋œ ๋ฌธ์ž์—ด์— ํฌํ•จํ•  ๋ฌธ์ž ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • str.substring(indexStart[, indexEnd])
  • str.substr(start[, length])
1
2
3
4
5
const text = 'helloworld'
//2๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•˜์—ฌ 5๋ฒˆ์งธ ์ธ๋ฑ์Šค ์ „๊นŒ์ง€ ์ถ”์ถœ
console.log(text.substring(2, 5)) // => "llo"
//2๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•˜์—ฌ 3๊ฐœ๊นŒ์ง€ ์ถ”์ถœ
console.log(text.substr(2, 3)) // => "llo"

substring()์™€ slice()์˜ ์ฐจ์ด์ 

substring()์™€ slice()๋Š” ๊ฑฐ์˜ ๋˜‘๊ฐ™์€๋ฐ, ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ ์ด ์กด์žฌํ•œ๋‹ค. ํŠนํžˆ ๋ถ€์ •์ ์ธ ์ธ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์— ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

  • substring(): indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํด ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์ธ์ˆ˜๋ฅผ ๋ฐ”๊พผ๋“ฏ์ด ์ž‘๋™ํ•œ๋‹ค.
  • slice(): indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํด ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
1
2
3
4
5
const text = 'helloworld'
//indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— text.substring(2,5)์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค.
console.log(text.substring(5, 2)) // => "llo"
//indexStart๊ฐ€ indexEnd๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋นˆ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
console.log(text.slice(5, 2)) // => ""

๋‘ ๊ฐ€์ง€ ์ธ์ˆ˜ ๋ชจ๋‘ ์Œ์ˆ˜๊ฑฐ๋‚˜ NaN์ธ ๊ฒฝ์šฐ, substring()

  • substring(): ๋‘ ๊ฐ€์ง€ ์ธ์ˆ˜ ๋ชจ๋‘ ์Œ์ˆ˜๊ฑฐ๋‚˜ NaN์ธ ๊ฒฝ์šฐ ์ธ์ˆ˜๊ฐ€ 0์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌํ•œ๋‹ค.
1
2
3
4
//substring(0, 2)์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌ
console.log(text.substring(-5, 2)) // => "he"
//substring(0)์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌ
console.log(text.substring(-5, -2)) // => ""
  • slice(): ๋‘ ๊ฐ€์ง€ ์ธ์ˆ˜ ๋ชจ๋‘ ์Œ์ˆ˜๊ฑฐ๋‚˜ NaN์ธ ๊ฒฝ์šฐ NaN์ธ์ˆ˜๋Š” 0์ฒ˜๋Ÿผ ์ทจ๊ธ‰ํ•˜์ง€๋งŒ, ์Œ์ˆ˜์ผ ๋•Œ๋Š” ๋ฌธ์ž์—ด ๋์—์„œ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.
1
2
3
4
5
6
7
//text.slice(0,0)์ฒ˜๋Ÿผ ์ทจ๊ธ‰
console.log(text.slice(-5, 2)) // => ""

// indexStart๊ฐ€ -5์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด ๊ธธ์ด 10 - 5์˜ 5๋ฒˆ์งธ์—์„œ ์‹œ์ž‘
// indexEnd๊ฐ€ -2์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด ๊ธธ์ด 10์—์„œ 10 - 2์˜ 8๋ฒˆ์งธ์—์„œ ์ถ”์ถœ ์ข…๋ฃŒ.
// text.slice(5, 8)๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ
console.log(text.slice(-5, -2)) // => "wor"

slice

๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์œผ๋ฉฐ, ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ์ง์ „๊นŒ์ง€ ์ถ”์ถœํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
const str = 'The quick brown fox jumps over the lazy dog.'

console.log(str.slice(31))
// expected output: "the lazy dog."

console.log(str.slice(4, 19))
// expected output: "quick brown fox"

console.log(str.slice(-4))
// expected output: "dog."

console.log(str.slice(-9, -5))
// expected output: "lazy"

str.slice(beginIndex[, endIndex])
str.slice(์ถ”์ถœ์‹œ์ž‘์  ์ธ๋ฑ์Šค[, ์ข…๋ฃŒ์ธ๋ฑ์Šค, ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค ์ง์ „๊นŒ์ง€ ์ถ”์ถœ])
return ๊ฐ’ : ์ถ”์ถœ๋œ ๋ถ€๋ถ„์„ ๋‹ด๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด

  • beginIndex
    • ์Œ์ˆ˜๊ฐ€ ์˜ฌ ๊ฒฝ์šฐ, strLength(๋ฌธ์ž์—ด ๊ธธ์ด) + beginIndex๋กœ ์ทจ๊ธ‰ (e.g. beginIndex๊ฐ€ -3์ด๋ฉด ์‹œ์ž‘์ ์€ strLength -3)
    • beginIndex๊ฐ€ strLength๋ณด๋‹ค ํฌ๋ฉด ๋นˆ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
  • endIndex
    • ์ถ”์ถœ ์ข…๋ฃŒ์  ์ธ๋ฑ์Šค ์œ„์น˜์˜ ๋ฌธ์ž๋Š” ์ถ”์ถœ์— ํฌํ•จ๋˜์ง€ ์•Š๊ณ , ๊ทธ ์ง์ „๊นŒ์ง€ ์ถ”์ถœ๋œ๋‹ค.
    • endIndex๊ฐ€ ์ƒ๋žต๋˜์—ˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ถ”์ถœํ•œ๋‹ค.
    • ์Œ์ˆ˜๋ผ๋ฉด endIndex๋Š” strLength + endIndex๋กœ ์ทจ๊ธ‰๋œ๋‹ค. (e.g. endInedx๊ฐ€ -3์ด๋ฉด ์ข…๋ฃŒ์ ์€ strLength -3)
1
2
3
4
5
6
7
8
9
10
11
12
13
const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice(1, 8),
str3 = str1.slice(4, -2),
str4 = str1.slice(12),
str5 = str1.slice(30)

console.log(str2) // OUTPUT: he morn
console.log(str3) // OUTPUT: morning is upon u
console.log(str4) // OUTPUT: is upon us.
console.log(str5) // OUTPUT: ""

//slice๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์€ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— str1์€ ๊ทธ๋Œ€๋กœ์ด๋‹ค.
console.log(str1) // OUTPUT: The morning is upon us.

์Œ์ˆ˜ ์ธ๋ฑ์Šค๋กœ slice()์‚ฌ์šฉ

1
2
3
4
const str = 'The morning is upon us.'
str.slice(-3) // returns 'us.'
str.slice(-3, -1) // returns 'us'
str.slice(0, -1) // returns 'The morning is upon us'
  • beginIndex๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ
    beginIndex๋Š” ๋ฌธ์ž์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ 11๊ฐœ๋ฅผ ์„ธ๊ณ , endIndex๋Š” ์‹œ์ž‘์ ๋ถ€ํ„ฐ ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ 16๊ฐœ๋ฅผ ์„ผ๋‹ค.
1
console.log(str.slice(-11, 16)) // => "is u";
  • endIndex๊ฐ€ ์Œ์ˆ˜์ผ ๋•Œ
    beginIndex๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์ ๋ถ€ํ„ฐ ์ˆœ๋ฐฉํ–ฅ์œผ๋กœ 11๊ฐœ๋ฅผ ์„ธ๊ณ , endIndex๋Š” ๋์—์„œ๋ถ€ํ„ฐ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ 7๊ฐœ๋ฅผ ์„ผ๋‹ค.
1
console.log(str.slice(11, -7)) // => "is u";
  • beginIndex, endIndex ๋ชจ๋‘ ์Œ์ˆ˜์ผ ๋•Œ
    beginIndex๋Š” ๋์—์„œ๋ถ€ํ„ฐ 5๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์‹œ์ž‘์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•˜๊ณ , ๋์—์„œ๋ถ€ํ„ฐ 1๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š”๋‹ค. beginIndex๊ฐ€ -5์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด ๊ธธ์ด 23 - 5์˜ 18๋ฒˆ์งธ์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. endIndex๋Š” -1์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด ๊ธธ์ด 23์—์„œ 23 - 1์˜ 22๋ฒˆ์งธ์—์„œ ์ถ”์ถœ์„ ์ข…๋ฃŒํ•œ๋‹ค.
1
console.log(str.slice(-5, -1)) // => "n us";

References
String.prototype.substr()
String.prototype.substring()
String.prototype.slice()