ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • slice
    Study/JavaScript 2020. 6. 24. 13:36

     

    정의와 사용


    slice() 메소드는 string의 부분을 추출하고 새로운 string에 추출된 부분을 리턴한다.

     

    우리가 추출하기 원하는 string의 부분을 명료화하기위해 start와 end 매개변수를 사용한다.

     

    첫번째 글자는 포지션이 0이다. 두번째는 1이다. 이런식으로 index값을 갖는다.

     

    팁: 문자열 끝에서 부터 사용하려면 음수를 사용하라.  

     

     

    매개변수 값


    Parameter Description
    start Required. The position where to begin the extraction. 
    First character is at position 0.
    end Optional. 
    The position (up to, but not including) where to end the extraction. 
    If omitted, slice() selects all characters from the start-position to the end of the string.

     

     

    More Examples


    let str = "Hello world!";
    let res = str.slice(0);
    
    console.log(res);  // Hello world!
    
    
    res = str.slice(3);
    console.log(res);  // lo world!
    
    res = str.slice(3, 8);
    console.log(res);  // lo wo
    
    res = str.slice(0, 1);
    console.log(res);  // H
    
    res = str.slice(-1);
    console.log(res);  // !

     

     

     

     

    출처: https://www.w3schools.com/jsref/jsref_slice_string.asp

    'Study > JavaScript' 카테고리의 다른 글

    날짜와 시간  (0) 2020.06.24
    string, Number 변환  (0) 2020.06.24
    배열의 최소값 구하기:: for문 알아보기  (0) 2020.06.24
    web API :: Navigator  (0) 2020.06.13
    Array.from()  (0) 2020.06.13

    댓글

Designed by Tistory.