我有一系列字符串,称为食物,
其中
food[0] = "banana fruit"
food[1] = "potato vegetable"
food[2] = "peas vegetable"
food[3] = "apple fruit"
food[4] = "pumpkin vegetable"
我如何打印只是蔬菜?
答案 0 :(得分:3)
您可以使用endsWith()
或contains()
字符串方法:
for (String s : food) {
if (s.endsWith("vegetable"))
System.out.println(s);
}