JavaScript

JavaScript - string 끝에서부터 자르기 (slice)

codeManager 2022. 5. 30. 18:26
반응형

자바스크립트에서 String 일부를 자르고 싶을 때 slice() 함수를 사용한다.

 

 

String.prototype.slice()

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

 

example

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

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"

 

 

출처

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

 

String.prototype.slice() - JavaScript | MDN

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

developer.mozilla.org

 

반응형