λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
JAVASCRIPT

[JAVASCRIPT] λ¬Έμžμ—΄ λ©”μ„œλ“œ : Slice(), substring(), substr()

by _토맀토 2022. 8. 17.
728x90

μžλ°”μŠ€ν¬λ¦½νŠΈ : λ¬Έμžμ—΄ λ©”μ„œλ“œ
slice(), substring(), substr()

slice(), substring(), substr()λŠ” λ¬Έμžμ—΄ λ©”μ„œλ“œμ˜ λ³€κ²½ νƒ€μž…μœΌλ‘œ λ¬Έμžμ—΄μ—μ„œ μ›ν•˜λŠ” 값을 μΆ”μΆœν•˜μ—¬ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.


#1. slice( )

slice() λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄μ—μ„œ μ›ν•˜λŠ” 값을 μΆ”μΆœν•˜μ—¬ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

! slice( ) μ‚¬μš© 방법 !
"λ¬Έμžμ—΄"slice(μ‹œμž‘ μœ„μΉ˜)
"λ¬Έμžμ—΄"slice(μ‹œμž‘ μœ„μΉ˜, λλ‚˜λŠ” μœ„μΉ˜)

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, 1);   //값이 λ‚˜μ˜€μ§€ μ•ŠλŠ”λ‹€. (λλ‚˜λŠ” μœ„μΉ˜λŠ” μ‹œμž‘ μœ„μΉ˜λ³΄λ‹€ 항상 컀야 ν•œλ‹€.)
const currentStr8 = str1.slice(1, 2);   //a
const currentStr9 = str1.slice(1, 3);   //av
const currentStr10 = str1.slice(1, 4);  //avs
const currentStr11 = str1.slice(-1);    //e : 음수면 λ’€μ—μ„œ λΆ€ν„° 좜λ ₯λœλ‹€.
const currentStr12 = str1.slice(-2);    //ce : 음수면 λ’€μ—μ„œ λΆ€ν„° 좜λ ₯λœλ‹€.
const currentStr13 = str1.slice(-3);    //nce : 음수면 λ’€μ—μ„œ λΆ€ν„° 좜λ ₯λœλ‹€.
const currentStr14 = str1.slice(-3, -1); //nc : μŒμˆ˜λŠ” μˆ˜κ°€ μž‘μ„ 수둝 큰 숫자이기 λ•Œλ¬Έμ— μ£Όμ˜ν•΄μ„œ μ‚¬μš©!
const currentStr15 = str1.slice(-3, -2); //n : μŒμˆ˜λŠ” μˆ˜κ°€ μž‘μ„ 수둝 큰 숫자이기 λ•Œλ¬Έμ— μ£Όμ˜ν•΄μ„œ μ‚¬μš©!
const currentStr16 = str1.slice(-3, -3); //값이 λ‚˜μ˜€μ§€ μ•ŠλŠ”λ‹€. (λλ‚˜λŠ” μœ„μΉ˜λŠ” μ‹œμž‘ μœ„μΉ˜λ³΄λ‹€ 항상 컀야 ν•œλ‹€.)

! point ! μ‹œμž‘ μœ„μΉ˜μ˜ 값은 λλ‚˜λŠ” μœ„μΉ˜μ˜ 값보닀 μž‘μ•„μ•Ό ν•©λ‹ˆλ‹€.


#2. substring( )

substring() λ©”μ„œλ“œλŠ” slice()와 λ§ˆμ°¬κ°€μ§€λ‘œ λ¬Έμžμ—΄μ—μ„œ μ›ν•˜λŠ” 값을 μΆ”μΆœν•˜μ—¬ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ substring()은 μ‹œμž‘ 값이 λλ‚˜λŠ” 값보닀 클 경우 μžλ™μœΌλ‘œ 두 값을 λ°”κΏ”μ„œ μ²˜λ¦¬ν•΄μ£ΌκΈ° λ•Œλ¬Έμ— μ—λŸ¬κ°€ λ‚˜μ§€ μ•ŠλŠ”λ‹€λŠ” νŠΉμ§•μ΄ μžˆμŠ΅λ‹ˆλ‹€.

! substring( ) μ‚¬μš© 방법 !
"λ¬Έμžμ—΄"substring(μ‹œμž‘ μœ„μΉ˜)

const str1 = "javascript reference"

const currentStr1 = str1.substring(4, 1); //ava : λλ‚˜λŠ” 값이 μ‹œμž‘ 값보닀 μž‘κΈ° λ•Œλ¬Έμ— 두 값을 λ°”κΏ”μ„œ μ²˜λ¦¬ν•¨
const currentStr2 = str1.substring(1, 4); //ava 

//slice()와 비ꡐ
const currentStr18 = str1.slice(4, 1);  //slice()은 μžλ™μœΌλ‘œ μ²˜λ¦¬κ°€ λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ—λŸ¬κ°€ λ– μ„œ 값이 λ‚˜μ˜€μ§€ μ•ŠλŠ”λ‹€.

! point ! μ‹œμž‘ 값이 λλ‚˜λŠ” 값보닀 클 경우 μžλ™μœΌλ‘œ 두 값을 λ°”κΏ”μ„œ μ²˜λ¦¬ν•΄μ£ΌκΈ° λ•Œλ¬Έμ— μ—λŸ¬κ°€ λ‚˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.


#3. substr( )

substr() λ©”μ„œλ“œλŠ” slice()와 거의 λ™μΌν•œ λ©”μ„œλ“œ μž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ μ›Ή ν‘œμ€€μ—μ„œ μ œμ™Έλœ λ©”μ„œλ“œμ΄κΈ° λ•Œλ¬Έμ— 잘 μ‚¬μš©ν•˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€.

! substr( ) μ‚¬μš© 방법 !
"λ¬Έμžμ—΄"substr(μ‹œμž‘ μœ„μΉ˜, 길이값)

const str1 = "javascript reference"

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

λŒ“κΈ€


Lucky Charms Rainbow
js
html
css