[JavaScript] 문자열을 배열로 변환하는 방법 (.split())

.split() 메서드

string.split(separator, limit)

separator : 문자를 분할하는 기준

limit : 배열 요소의 최대 개수 (지정 개수 이후의 문자는 담지 않음)

 

 

매개변수 separator 에 빈 문자열("")을 지정하면 모든 문자를 따로 배열에 담을 수 있음

var strValue = "hello"
strValue = strValue.split("");

console.log(strValue); //['h', 'e', 'l', 'l', 'o']

 

매개변수 separator 에 쉼표, 하이픈 등을 넣을 수 있음

var strValue = "hello, world"
strValue = strValue.split(',');

console.log(strValue); //['hello', 'world']

 

매개변수 separator 에 아무것도 넣지 않으면 하나의 요소로 구성된 배열을 반환

var strValue = "hello, world"
strValue = strValue.split();

console.log(strValue); //['hello, world']

 

 

 

참고자료:

https://developer-talk.tistory.com/808

https://www.codingfactory.net/10424