2017-07-04 91 views

回答

1

做了一些改變:

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

split該方法返回一個數組。而不是使用它的結果,你會得到原始字符串的第一個元素。

+0

改變工作 –

+0

很好。樂意效勞。 –

+0

如果解決了您的問題,請隨時將答案標記爲已完成。它也會幫助其他人。 –

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

首先,stringToSplitStringToSplit是不一樣的。 JS是區分大小寫的。你也不會保存StringToSplit.split(" ")的結果,然後你只輸出字符串stringToSplit的第一個字符,即a。你可以這樣做:

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。它也比JavaScript更像JavaScript或者Angular。