λ¬Έμμ΄ λ³κ²½ λ©μλ : slice() / substring() / substr()
slice(), substring(), substr() λ©μλλ λ¬Έμμ΄μμ μνλ κ°μ μΆμΆνμ¬ λ¬Έμμ΄λ‘ λ°νν©λλ€. μΈ κ°μ§ λ©μλλ λͺ¨λ λΉμ·νλ©΄μλ, μλ λ°©λ²μ μ‘°κΈμ© λ€λ¦ λλ€.
slice() λ©μλ
slice() λ©μλλ μ΄λ€ λ°°μ΄μ beginλΆν° endκΉμ§(end λ―Έν¬ν¨)μ λν 볡μ¬λ³Έμ λ°νν©λλ€. λ κ°μ§ λ°©λ²μΌλ‘ μλμν¬ μ μμ΅λλ€.
1. slice : "λ¬Έμμ΄".slice(μμ μμΉ)
const str1 = "javascript reference";
const currentStr1 = str1.slice(0);
const currentStr2 = str1.slice(1);
const currentStr3 = str1.slice(2);
결과보기
avascript reference
vascript reference
2. slice : "λ¬Έμμ΄".slice(μμ μμΉ, λλλ μμΉ)
μ΄λ, λλλ μμΉμ κ°μ΄ μμ μμΉμ κ°λ³΄λ€ μ»€μΌ κ°μ΄ μΆλ ₯λ©λλ€. μμΉκ°μ μμλ‘ μ λ ₯ν μλ μμ΅λλ€.
const str1 = "javascript reference";
const currentStr1 = str1.slice(0, 1);
const currentStr2 = str1.slice(0, 2);
const currentStr3 = str1.slice(0, 3);
const currentStr4 = str1.slice(-1);
const currentStr5 = str1.slice(-2);
const currentStr6 = str1.slice(-3, -1);
const currentStr7 = str1.slice(-3, -2);
결과보기
ja
jav
e
ce
nc
n
substring() λ©μλ
substring()μ slice() λ©μλμ λμΌνκ² μλν©λλ€. κ·Έλ¬λ slice() λ©μλμ λ¬λ¦¬, μμ μμΉκ°μ΄ λλλ μμΉκ°λ³΄λ€ ν΄ κ²½μ°, κ°μ μΈμνμ¬ λ κ°μ λ°κΎΈμ΄ μ²λ¦¬ν΄ μ€λλ€. μ¦, slice()μμ λνλ μ μλ μ€λ₯λ₯Ό λ°©μ§ν μ μλ λ©μλμ λλ€.
const str1 = "javascript reference";
const currentStr1 = str1.slice(1, 4);
const currentStr2 = str1.slice(4, 1); // μΆλ ₯λμ§ μμ΅λλ€.
const currentStr3 = str1.substring(4, 1); // κ°μ λ°κΎΈμ΄ μΆλ ₯ν΄μ€λλ€.
const currentStr4 = str1.substring(1, 4);
결과보기
X
ava
ava
substr() λ©μλ
substr() λ©μλ λν μ λ©μλλ€κ³Ό μ μ¬νμ§λ§, μ λ ₯νλ κ°μ΄ μ‘°κΈ λ€λ¦ λλ€. λ§μ°¬κ°μ§λ‘ λ κ°μ§ λ°©λ²μΌλ‘ μλμν¬ μ μμ΅λλ€.
1. substr() : "λ¬Έμμ΄".substr(μμ μμΉ)
const str1 = "javascript reference";
const currentStr1 = str1.substr(0);
const currentStr2 = str1.substr(1);
const currentStr3 = str1.substr(2);
결과보기
avascript reference
vascript reference
2. substr() : "λ¬Έμμ΄".substr(μμ μμΉ, κΈΈμ΄)
const str1 = "javascript reference";
const currentStr1 = str1.substr(0, 1);
const currentStr2 = str1.substr(0, 2);
const currentStr3 = str1.substr(0, 3);
const currentStr4 = str1.substr(-1, 1);
const currentStr5 = str1.substr(-2, 2);
const currentStr6 = str1.substr(-3, 3);
결과보기
ja
jav
e
ce
nce
'Javascript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μλ°μ€ν¬λ¦½νΈ : λ¬Έμμ΄ λ³κ²½ λ©μλ : trim() / toUpperCase() / toLowerCase() (2) | 2022.08.17 |
---|---|
μλ°μ€ν¬λ¦½νΈ : λ¬Έμμ΄ κ²μ λ©μλ : indexOf() / lastIndexOf() (6) | 2022.08.17 |
μλ°μ€ν¬λ¦½νΈ : μ κ·μ νν (6) | 2022.08.17 |
μλ°μ€ν¬λ¦½νΈ : λ΄μ₯ν¨μ (8) | 2022.08.12 |
μλ°μ€ν¬λ¦½νΈ : λ°°μ΄ λ©μλ : join(), push(), pop() (8) | 2022.08.11 |
λκΈ