slice splice split replace

sohyun·2024년 2월 2일
0

Javascript basic

목록 보기
7/7

slice(startIndex, endIndex)

const str = 'Hello, World!';

// startIndex부터 endIndex 전까지의 문자열을 추출
const slicedStr = str.slice(0, 5);
console.log(slicedStr); // 'Hello'

splice(startIndex, deleteCount, item1, item2, ...)

const arr2 = [1, 2, 3, 4, 5];

// startIndex부터 deleteCount만큼의 요소를 제거하고, item1, item2, ...를 추가
arr2.splice(2, 2, 6, 7);
console.log(arr2); // [1, 2, 6, 7, 5]

split(separator)

const str2 = 'apple,orange,banana';

// separator를 기준으로 문자열을 나누어 배열로 반환
const fruits = str2.split(',');
console.log(fruits); // ['apple', 'orange', 'banana']

replace(searchValue, replaceValue)

const str3 = 'Hello, World!';
// 문자열에서 searchValue를 찾아 replaceValue로 대체
const newStr = str3.replace('World', 'Universe');
console.log(newStr); // 'Hello, Universe!'
profile
냠소현 개발일지

0개의 댓글