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

[JAVASCRIPT] λ¬Έμžμ—΄ λ©”μ„œλ“œ : split( )

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

 

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

이번 μ‹œκ°„μ—λŠ” λ¬Έμžμ—΄ λ©”μ„œλ“œμΈ split()에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€! 😁


#1. split( )

slice() λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄μ—μ„œ μ›ν•˜λŠ” 값을 μΆ”μΆœν•œ λ’€ μƒˆλ‘œμš΄ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€. 즉 λ¬Έμžμ—΄μ„ λΆ„ν• ν•˜μ—¬ λΆ€λΆ„ λ¬Έμžμ—΄ 배열을 λ§Œλ“­λ‹ˆλ‹€
πŸ“– μ•„λž˜μ˜ μ½”λ“œ μ˜ˆμ œμ™€ 주석을 톡해 더 μžμ„Ένžˆ μ•Œμ•„λ΄…μ‹œλ‹€! πŸ“–

! split( ) μ‚¬μš© 방법 !
"λ¬Έμžμ—΄".split(κ΅¬λΆ„μž(separator));
"λ¬Έμžμ—΄".split(μ •κ·œμ‹ ν‘œν˜„);
"λ¬Έμžμ—΄".split(κ΅¬λΆ„μž, 갯수); //갯수λ₯Ό μ„€μ •ν•  수 μžˆλ‹€.

const str1 = "javascript reference";
const currentStr1 = str1.split('');             //['j','a','v','a','s','c','r','i','p','t',' ','r','e','f','e','r','e','n','c','e']
const currentStr2 = str1.split(' ');            // ['javascript', 'reference']
const currentStr3 = str1.split('', 1);          // ['j']
const currentStr4 = str1.split('', 2);          // ['j', 'a']
const currentStr5 = str1.split(' ', 1);         // ['javascript']
const currentStr6 = str1.split(' ', 2);         // ['javascript', 'reference']
const currentStr7 = str1.split('j');            // ['', 'avascript reference']
const currentStr8 = str1.split('a');            // ['j', 'v', 'script reference']
const currentStr9 = str1.split('e');            // ['javascript r', 'f', 'r', 'nc', '']


const str2 = "java/script/refer/ence";
const currentStr10 = str2.split('/');       //['java', 'script', 'refer', 'ence']


const str3 = "java&script&refer!ence";
const currentStr11 = str3.split('!');       // ['java&script&refer', 'ence'] !의 μœ„μΉ˜λ₯Ό κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄μ΄ λ‚˜λ‰˜μ–΄ 진닀.
const currentStr12 = str3.split('&');       // ['java', 'script', 'refer!ence'] &의 μœ„μΉ˜λ₯Ό κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄μ΄ λ‚˜λ‰˜μ–΄μ§€λ©° &(μ•€λ“œ)기호 κΉŒμ§€λŠ” 찾지 λͺ»ν•œλ‹€.
const currentStr13 = str3.split(/&|\!/);    // ['java', 'script', 'refer', 'ence'] : /&|\!/ 와 같이 μ •κ·œμ‹ ν‘œν˜„λ„ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€.


const str4 = "javascript reference";
const currentStr14 = str4.split('').join();              // e,c,n,e,r,e,f,e,r, ,t,p,i,r,c,s,a,v,a,j (λ°°μ—΄ 사라지고 λ°˜λŒ€λ‘œ 좜λ ₯.)
const currentStr15 = str4.split('').join('*');           // j*a*v*a*s*c*r*i*p*t* *r*e*f*e*r*e*n*c*e 
const currentStr16 = str4.split('').reverse().join();    // e,c,n,e,r,e,f,e,r, ,t,p,i,r,c,s,a,v,a,j (λ°°μ—΄ 사라지고 λ°˜λŒ€λ‘œ 좜λ ₯.)
const currentStr17 = str4.split('').reverse().join('/'); // e/c/n/e/r/e/f/e/r/ /t/p/i/r/c/s/a/v/a/j

! point ! /&|\!/ 와 같이 μ •κ·œμ‹ ν‘œν˜„λ„ μ‚¬μš© κ°€λŠ₯!
νŠΉμ • 문자 λ˜λŠ” λ¬Έμžμ—΄μ—μ„œ λ¬Έμžμ—΄μ„ λΆ„ν• ν•˜λŠ” 데도 μ‚¬μš©!

728x90

λŒ“κΈ€


Lucky Charms Rainbow
js
html
css