ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열 다루기
    JavaScript 2020. 5. 11. 22:31

    concat( )

    : 매개변수로 전달된 모든 문자열을 호출 문자열에 붙인 새로운 문자열을 반환

    let str1 = 'hi';
    let str2 = 'bless you';
    str1.concat(' ', str2);
    // "hi bless you"

     

    str.indexOf(searchValue)

    : 호출한 String 객체에서 주어진 값과 일치하는 첫 번째 인덱스를 반환. 일치하는 값이 없으면 -1을 반환

    'hello world'.indexOf('world'); // 6
    'hello world'.indexOf('Hello'); // -1 (대소문자를 구분함)

     

    str.includes(searchValue)

    : 하나의 문자열이 다른 문자열에 포함되어 있는지를 판별하고, 결과를 true 또는 false 로 반환

    'blue sky'.includes('blue') // true
    'blue sky'.includes('moon') // false

     

    str.split(seperator)

    : 문자열을 지정한 구분자를 이용하여 여러 개의 문자열로 나눔.

    - 인자 : 분리 기준이 될 문자열

    - 반환 값 : 분리된 문자열이 포함된 배열

    - csv 형식을 처리할 때 유용

    let str = 'hello world';
    console.log(str.split(' '));
    // ['hello', 'world']

     

    str.substring(start, end)

    : string 객체의 시작 인덱스로 부터 종료 인덱스 전 까지 문자열의 부분 문자열을 반환

    let str = 'heyheyjude';
    console.log(str.substring(0, 3)); // 'hey'
    console.log(str.substring(-1, 3)); // 'hey', 음수는 0으로 취급
    console.log(str.substring(0, 20)); // 'heyheyjude' // index 범위를 넘을 경우 마지막 index로 취급

     

    str.slice( )

    : 문자열의 일부를 추출하면서 새로운 문자열을 반환

    - substirng과 비슷하나, 몇가지 차이점이 있음

    'JavaScript' 카테고리의 다른 글

    반복문(for, while)  (0) 2020.05.12
    숫자 다루기  (0) 2020.05.12
    배열로 함수형 프로그래밍하기  (0) 2020.05.11
    객체(object) 다루기  (0) 2020.05.10
    객체, 배열과 관련된 메소드들  (0) 2020.05.09

    댓글

Designed by Tistory.