[JS] string.substring 메소드

SINGING BIRD·2023년 4월 8일
  • 문자열의 부분 문자열을 구하는 함수입니다.
<script>
const string = 'dream is my reality'

console.log(string.substring(6))
// index 6 부터 ~ 끝까지 가져옵니다.
// 'is my reality'

console.log(string.substring(6, 8))
// index 6 부터 ~ index 8 '직전' 까지 가져옵니다.
// 'is'
</script>

  • 문자열이 있을 때, 특정 인덱스의 문자를 가져올 때 사용할 수 있습니다.
<script>
const menu = 'abcdefghijklmnopqrstuvwxyz';
const letterIndex = 5;

console.log(menu.substring(letterIndex, letterIndex + 1);
// index 5 부터 index 6 이전까지 가져옵니다.
// 'f'

</script>
profile
good things take time

0개의 댓글