我有一个List<String>
可以包含空白("")
。删除字符串列表中的空白
有没有简单的方法从列表中删除所有的空白?我用这种方式来做到这一点。
ListIterator<String> it = values.listIterator();
while (it.hasNext()) {
if (it.next().equals("")) {
it.remove();
}
}
感谢您的帮助!
我有一个List<String>
可以包含空白("")
。删除字符串列表中的空白
有没有简单的方法从列表中删除所有的空白?我用这种方式来做到这一点。
ListIterator<String> it = values.listIterator();
while (it.hasNext()) {
if (it.next().equals("")) {
it.remove();
}
}
感谢您的帮助!
我发现了一个解决方案上how to remove blank items from ArrayList.Without removing index wise
removeAll(Collection<?> c)
这工作正常!
List<C>
中有一个方法,名为removeAll(Collections <?> c)
,它能够删除所有元素。
它会从列表中删除
values.removeAll(Arrays.asList("")); //remove all blank String
的Java 8中添加一个优雅的removeIf
方法的所有空白字符串:
values.removeIf(String::isEmpty);
列表条目为空或在列表中的条目包含在两者之间的空白? – piyushj
可能的重复http://stackoverflow.com/questions/5520693/in-java-remove-empty-elements-from-a-list-of-strings – Shravan40