[String] split()

Sejung Seo·2022년 6월 23일
0

JavaScript

목록 보기
19/19
post-thumbnail

split()

split() 메서드는 String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눕니다.

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
// split(' ')는 단어단위로 나눠집니다. 아래와같이 인덱스3번으로 지정시, 인덱스3번 객체인 "fox"가 출력됩니다.
console.log(words[3]);	// output: "fox"
console.log(word)		// output: (9) ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']

const chars = str.split('');
//// split('')는 알파벳단위로 나눠집니다. 아래와같이 인덱스8번으로 지정시, 인덱스8번 객체인 "k"가 출력됩니다.
console.log(chars[8]);	// output: "k"
console.log(chars);		// output: (44) ['T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g', '.']

const strCopy = str.split();
//split()는 문자열을 전부 하나의 객체로 만들어집니다.
console.log(strCopy);	// output: Array ["The quick brown fox jumps over the lazy dog."]
profile
공부하는 코린이 🌼

0개의 댓글