λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μžλ°”μŠ€ν¬λ¦½νŠΈ

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

λŒ“κΈ€