[JS] 마지막 문자열 제거

1. slice()

const str = "Hello, World, JavaScript,";

let newStr = str.slice(0, -1);
console.log(newStr);

newStr = str.slice(0, str.length - 1);
console.log(newStr);

2. substring()

const str = "Hello, World, JavaScript,";

const newStr = str.substring(0, str.length - 1);
console.log(newStr);

3. substr()

const str = "Hello, World, JavaScript,";

const newStr = str.substr(0, str.length - 1);
console.log(newStr);

4. replace()와 정규표현식

const str = "Hello, World, JavaScript,";

const newStr = str.replace(/,$/, '');
console.log(newStr);

 

guest
0 Comments
Inline Feedbacks
View all comments