使用整数Arraylist从字符串中提取单词

时间:2017-01-12 05:18:21

标签: java arraylist indexing

如何使用提取arraylist来获取文本字符串中该索引处的单词?

ArrayList<Interger> extract = new ArrayList<Interger>();

extract.add(22); extract.add(37);

String text ="How do I get the this word? And this word?";

结果应该是(单词,单词)

1 个答案:

答案 0 :(得分:4)

List<String> words = new ArrayList<>();
for (int index : extract) {
    words.add(text.substring(index).split("\\b")[0]);
}
System.out.println(words);
// [word, word]