728x90
split()이란?
: 구분자를 기준으로 문자열을 잘라 배열로 입력할 때 사용하는 메서드
출력방법
"문자열".split(구분자);
"문자열".split(정규식 표현 가능);
"문자열".split(구분자, 갯수);
"문자열".split(구분자);
"문자열".split(정규식 표현 가능);
"문자열".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() //j,a,v,a,s,c,r,i,p,t, ,r,e,f,e,r,e,n,c,e join으로 배열 없어지고 문자열로 출력됨
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() //,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
//console.log(currentStr17);
댓글