string split, slice, Array splice

blossom0417
2 min readSep 4, 2018

--

#slice(n)

  • n = 추출될 갯수 . — return current string - n 갯수
  • Required. The position where to begin the extraction. First character is at position 0
var str = "Hello world!"; 
var res = str.slice(1);
//ello world!
//H 빼고 추출하여 프린트
  • slice(n,m) — start,end — return new string
  • start-
  • 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
  • 추출하고픈 string
var str = "Hello world!"; 
var res = str.slice(3, 8);
//lo wo
3번째부터 8번째까지. 8번째 포함안됨.
var str = "Hello world!";
var res = str.slice(0,1);
//H

# split(n,m)- The split() method is used to split a string into an array of substrings, and returns the new array.

  • Return Value:An Array, containing the splitted values
  • Split a string into an array of substrings:
var str = "How are you doing today?";
var res = str.split(" ");
//how,are,you,doing,today?
//' '로 분리
var str = "How are you doing today?";
var res = str.split(" ").slice(1);
//are,you,doing,today?
var str = "How are you doing today?";
var res = str.split("");
//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
var str = "How are you doing today?";
var res = str.split(" ", 3);
//how,are,you
//' ' 로 분리하고 3개만 추출

#splice — The splice() method adds/removes items to/from an array, and returns the removed item(s).

Note: This method changes the original array.

  • splice(index,howmany, item1…)
  • howmany - Optional. The number of items to be removed. If set to 0, no items will be removed
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
//Banana,Orange,Lemon,Kiwi,Apple,Mango
fruits.splice(-2,2); // 끝에서 2번째 2개 아이템 삭제
//Banana,Orange

#Array.join() — separated by the specified separator

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.join(" and ");
//Banana and Orange and Apple and Mango

#includes() -The includes() method determines whether an array contains a specified element.

This method returns true if the array contains the element, and false if not.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");
//true

--

--

No responses yet