离子2 - 将句子字符串转换为单词数组

时间:2018-01-06 15:52:22

标签: javascript typescript ionic-framework ionic2 ionic3

我有一串单词(用新行分隔)

aa
aaa
aaaa
aaaaa

我希望将它添加到数组"单词"

[1] = aa
[2] = aaa
[3] = aaaa
[4] = aaaaa

我怎么能在离子2中做到这一点?我在java中知道它看起来像这样

String s = "This is a sample sentence.";
String[] words = s.split("\\s+");
for (int i = 0; i < words.length; i++) {

    words[i] = words[i].replaceAll("[^\\w]", "");
}

但无法在离子2中弄清楚。

1 个答案:

答案 0 :(得分:1)

在Javascript或Typescript中你也可以这样做:

 const s = "This is a sample sentence.";
 const words = s.split(" ");
 
 // displaying the array
 for (word of words){
 console.log(word);
 }