μλ°μ€ν¬λ¦½νΈ : λ¬Έμμ΄ λ©μλ
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
'JAVASCRIPT' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JAVASCRIPT] λ¬Έμμ΄ λ©μλ : indexOf( ) (5) | 2022.08.17 |
---|---|
[JAVASCRIPT] μ κ· ννμ (4) | 2022.08.17 |
[JAVASCRIPT] λ΄μ₯ ν¨μ (4) | 2022.08.16 |
[JAVASCRIPT] λ°°μ΄ λ©μλ : join(), push(), pop() (4) | 2022.08.11 |
μμ μ ν (3) | 2022.08.07 |
λκΈ