문자열 기초

프론트엔드 꿈나무·2022년 8월 23일
  • str.indexof(serchValue)

처음으로 일치하는 index,찾고자하는 문자열이 없으면 -1을 반환
'Computer Game'.indexof('Computer'); // 0
'Computer Game'.indexof('computer'); // -1
'Computer Game'.indexof('Game'); // 10
'Computer Game Game'.indexof('Computer'); // 10

  • str.split(seperator)
    분리기준이 될 문자열을 인자값(argument)로 전달하여 분리된문자열을
    배열로 리턴
    *csv 형식을 처리할떄 유용

var str = 'Hello from the other side';
console.log(str.split(''));
['Hello','from','the','other','side']
줄바꿈을 기준으로 분리하고싶을땐 => str.split('\n')

  • str.substring(start,end)
    argument:시작index,끝index
    return value: 시작과 끝 index 사이의 문자열
    var str='abcd';
    console.log(str.sumstring(0,3)); // 'abc'
    console.log(str.sumstring(3,0)); // 'abc'
    인자값에 음수는 0으로 취급 또는 인자값을 초과하는 값은 인자값의 끝범위로 인식함.

0개의 댓글