๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

slice() / substring() / substr()

by Youcodein 2022. 8. 17.
728x90
๋ฐ˜์‘ํ˜•

1. ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : ๋ณ€๊ฒฝ : slice() : ๋ฌธ์ž์—ด์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถ”์ถœ

slice() : ๋ฌธ์ž์—ด์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

//slice(์‹œ์ž‘ ์œ„์น˜)
//slice(์‹œ์ž‘ ์œ„์น˜, ๋๋‚˜๋Š” ์œ„์น˜)//๋๋‚˜๋Š” ์œ„์น˜ ๊ฐ’์ด ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’๋ณด๋‹ค ์ปค์•ผํ•จ
//substr(์‹œ์ž‘์œ„์น˜)
//substr(์‹œ์ž‘์œ„์น˜, ๊ธธ์ด)
const str1 = "javascript reference";
const currentStr1 = str1.slice(0);  //javascript reference
const currentStr2 = str1.slice(1);  //avascript reference
const currentStr3 = str1.slice(2);  //vascript reference
const currentStr4 = str1.slice(0, 1);  //j
const currentStr5 = str1.slice(0, 2);  //ja
const currentStr6 = str1.slice(0, 3);  //jav
const currentStr7 = str1.slice(1, 2);  //av
const currentStr8 = str1.slice(1, 3);  //av
const currentStr9 = str1.slice(1, 4);  //avs
const currentStr10 = str1.slice(-1);  //e
const currentStr11 = str1.slice(-2);  //ce
const currentStr12 = str1.slice(-3);  //nce
const currentStr13 = str1.slice(-3, -1);  //nc
const currentStr14 = str1.slice(-3, -2);  //n
const currentStr15 = str1.slice(-3, -3);  //n

const currentStr16 = str1.slice(1, 4);  //ava
const currentStr17 = str1.slice(4, 1);  //''

const currentStr18 = str1.substring(4, 1);  //ava
const currentStr19 = str1.substring(1, 4);  //ava

//substring()์€ ์‹œ์ž‘ ์œ„์น˜์™€ ๋๋‚˜๋Š” ์œ„์น˜๊ฐ’์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์ด ์—†๋‹ค. ์ž๋™์œผ๋กœ ๋ฐ”๊ฟ”์คŒ

const currentStr20 = str1.substr(0);  //javascript reference
const currentStr21 = str1.substr(1);  //avascript reference
const currentStr22 = str1.substr(2);  //vascript reference
const currentStr23 = str1.substr(0, 1);  //j
const currentStr24 = str1.substr(0, 2);  //ja
const currentStr25 = str1.substr(0, 3);  //jav
const currentStr26 = str1.substr(1, 2);  //av
const currentStr27 = str1.substr(1, 3);  //ava
const currentStr28 = str1.substr(1, 4);  //avas
const currentStr29 = str1.substr(-1);  //e
const currentStr30 = str1.substr(-2);  //ce
const currentStr31 = str1.substr(-3);  //nce
const currentStr32 = str1.substr(-1, 1);  //e
const currentStr33 = str1.substr(-2, 2);  //ce
const currentStr34 = str1.substr(-3, 3);  //nce

02. ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : slice() substring() substr()

๋ฌธ์ž์—ด์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ถ”์ถœํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

"๋ฌธ์ง€์—ด".slice(์‹œ์ž‘ ์œ„์น˜)
"๋ฌธ์ง€์—ด".slice(์‹œ์ž‘ ์œ„์น˜, ๋๋‚˜๋Š” ์œ„์น˜)
//๋๋‚˜๋Š” ์œ„์น˜ ๊ฐ’์ด ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’๋ณด๋‹ค ์ปค์•ผํ•จ
//substring()์€ ์‹œ์ž‘๊ฐ’์ด ๋๋‚˜๋Š” ๊ฐ’๋ณด๋‹ค ํด ๊ฒฝ์šฐ ๋‘ ๊ฐ’์„ ๋ฐ”๊ฟ”์„œ ์ฒ˜๋ฆฌ(์—๋Ÿฌ๋ฐฉ์ง€) "๋ฌธ์ง€์—ด".substr(์‹œ์ž‘์œ„์น˜)
"๋ฌธ์ง€์—ด".substr(์‹œ์ž‘์œ„์น˜, ๊ธธ์ด)
//slice(์‹œ์ž‘ ์œ„์น˜)
//slice(์‹œ์ž‘ ์œ„์น˜, ๋๋‚˜๋Š” ์œ„์น˜)//๋๋‚˜๋Š” ์œ„์น˜ ๊ฐ’์ด ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’๋ณด๋‹ค ์ปค์•ผํ•จ
//substr(์‹œ์ž‘์œ„์น˜)
//substr(์‹œ์ž‘์œ„์น˜, ๊ธธ์ด)
const str1 = "javascript reference";
const currentStr1 = str1.slice(0);  //javascript reference
const currentStr2 = str1.slice(1);  //avascript reference
const currentStr3 = str1.slice(2);  //vascript reference
const currentStr4 = str1.slice(0, 1);  //j
const currentStr5 = str1.slice(0, 2);  //ja
const currentStr6 = str1.slice(0, 3);  //jav
const currentStr7 = str1.slice(1, 2);  //av
const currentStr8 = str1.slice(1, 3);  //av
const currentStr9 = str1.slice(1, 4);  //avs
const currentStr10 = str1.slice(-1);  //e
const currentStr11 = str1.slice(-2);  //ce
const currentStr12 = str1.slice(-3);  //nce
const currentStr13 = str1.slice(-3, -1);  //nc
const currentStr14 = str1.slice(-3, -2);  //n
const currentStr15 = str1.slice(-3, -3);  //n

const currentStr16 = str1.slice(1, 4);  //ava
const currentStr17 = str1.slice(4, 1);  //''

const currentStr18 = str1.substring(4, 1);  //ava
const currentStr19 = str1.substring(1, 4);  //ava

//substring()์€ ์‹œ์ž‘ ์œ„์น˜์™€ ๋๋‚˜๋Š” ์œ„์น˜๊ฐ’์˜ ํฌ๊ธฐ์— ์˜ํ–ฅ์ด ์—†๋‹ค. ์ž๋™์œผ๋กœ ๋ฐ”๊ฟ”์คŒ

const currentStr20 = str1.substr(0);  //javascript reference
const currentStr21 = str1.substr(1);  //avascript reference
const currentStr22 = str1.substr(2);  //vascript reference
const currentStr23 = str1.substr(0, 1);  //j
const currentStr24 = str1.substr(0, 2);  //ja
const currentStr25 = str1.substr(0, 3);  //jav
const currentStr26 = str1.substr(1, 2);  //av
const currentStr27 = str1.substr(1, 3);  //ava
const currentStr28 = str1.substr(1, 4);  //avas
const currentStr29 = str1.substr(-1);  //e
const currentStr30 = str1.substr(-2);  //ce
const currentStr31 = str1.substr(-3);  //nce
const currentStr32 = str1.substr(-1, 1);  //e
const currentStr33 = str1.substr(-2, 2);  //ce
const currentStr34 = str1.substr(-3, 3);  //nce
728x90
๋ฐ˜์‘ํ˜•

'์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

toUpperCase() / toLowerCase()  (4) 2022.08.17
์ •๊ทœํ‘œํ˜„์‹  (4) 2022.08.17
indexOf()/lastIndexOf()  (6) 2022.08.17
๋‚ด์žฅํ•จ์ˆ˜  (3) 2022.08.15
๋ฐฐ์—ด ๊ด€๋ จ ๋ฉ”์„œ๋“œ  (10) 2022.08.11

๋Œ“๊ธ€