在Java中,如何将子字符串与字符串数组进行比较

在Java中,如何将子字符串与字符串数组进行比较,java,Java,如何使用Java编程将子字符串与数组中提到的字符串进行比较? 检查AC是否在{苹果-AC、球、猫、医生……,} 这应该返回Apple-AC,因为它包含AC作为子字符串 所以,下次你在stackoverflow上发表文章时,一定要列出你以前尝试过的内容以及你遇到的问题,而不是要求我们完全为你做 public static String findWithSubstring(String[] arr, String sub) { for (int i=0; i<arr.length();

如何使用Java编程将子字符串与数组中提到的字符串进行比较? 检查AC是否在{苹果-AC、球、猫、医生……,}
这应该返回Apple-AC,因为它包含AC作为子字符串

所以,下次你在stackoverflow上发表文章时,一定要列出你以前尝试过的内容以及你遇到的问题,而不是要求我们完全为你做

public static String findWithSubstring(String[] arr, String sub) {
    for (int i=0; i<arr.length(); i++) {
        if (arr[i].contains(sub)) {
            return arr[i];
        }
    }
    return "";
}
因此,Thunder有正确的方法,但无论哪个元素中有子字符串,它都将返回true。这个问题的答案应该是Apple-AC

String[] words = {"Apple - AC", "Ball", "Cat", "Doctor"};
String filter = "AC";
如果需要所有匹配的单词:

List<String> list = Stream.of(words)
        .filter(word -> word.contains(filter))
        .collect(Collectors.toList());

System.out.println(list);
如果您只对第一个匹配的单词感兴趣:

Optional<String> firstMatch = Stream.of(words)
        .filter(word -> word.contains(filter))
        .findFirst();

System.out.println(firstMatch);

在你的行为上没有任何努力这属于给我密码的范畴。所以我建议你先自己尝试一些东西,当你有一个真正的问题或类似的问题时再回来