Split string based on spaces and read that in angular2

前端 未结 2 1966
有刺的猬
有刺的猬 2021-02-07 09:58

I am creating a pipe in angular2 where I want to split the string on white spaces and later on read it as an array.

let stringToSplit = \"abc def ghi\";
StringTo         


        
2条回答
  •  佛祖请我去吃肉
    2021-02-07 10:28

    let stringToSplit = "abc def ghi";
    StringToSplit.split(" ");
    console.log(stringToSplit[0]);
    

    First, stringToSplit and StringToSplit are not the same. JS is case sensitive. Also you dont save result of StringToSplit.split(" ") anywhere and then you just output the first character of the string stringToSplit which is a. You could do like this:

        let stringToSplit = "abc def ghi";
        console.log(stringToSplit.split(" ")[0]); // stringToSplit.split(" ") returns array and then we take the first element of the array with [0]

    PS. also it is more about JavaScript than TypeScript or Angular.

提交回复
热议问题