java在数组中的字符串中拉第二个单词

时间:2013-06-08 05:27:02

标签: java arrays string

我有一系列字符串,称为食物,
其中

food[0] = "banana fruit"
food[1] = "potato vegetable"
food[2] = "peas vegetable"
food[3] = "apple fruit"
food[4] = "pumpkin vegetable"

我如何打印只是蔬菜?

1 个答案:

答案 0 :(得分:3)

您可以使用endsWith()contains()字符串方法:

for (String s : food) {
  if (s.endsWith("vegetable"))
    System.out.println(s);
}