简体   繁体   English

检查字符串是否包含数组中的所有字符串

[英]Check if string contains all strings from array

How to check if String contains all Strings from Array .如何检查String包含来自Array所有Strings

My current code:我目前的代码:

String word = "abc";
String[] keywords = {"a", "d"};

for(int i = 0; i < keywords.length; i++){
    if(word.contains(keywords[i])){
       System.out.println("Yes");
    }else{
       System.out.println("No");   
    }
}

The code would look much more nicer if you wrap it into a separate method:如果您将其包装到一个单独的方法中,代码看起来会更好:

public static boolean containsAllWords(String word, String ...keywords) {
    for (String k : keywords)
        if (!word.contains(k)) return false;
    return true;
}

Use a boolean variable that will tell you if every keyword is matched.使用一个boolean变量来告诉你是否每个关键字都匹配。 Set it to true as default value.将其设置为true作为默认值。 Then check every keword: if any one is not contained in your word, stop searching and set your variable to false .然后检查每个关键字:如果您的单词中不包含任何关键字,请停止搜索并将变量设置为false

boolean containsAll = true; 
for (String keyword : keywords){
    if (!word.contains(keyword)){
       containsAll = false;
       break;
    }
}

If you are using Java 8+, you could use a Stream and test if all of the elements match your criteria with one line.如果您使用的是 Java 8+,则可以使用Stream并用一行测试所有元素是否符合您的条件。 Like,喜欢,

if (Stream.of(keywords).allMatch(word::contains)) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

In earlier versions, or if you want to understand what the above is doing, it might look something like在早期版本中,或者如果您想了解上面的内容,它可能看起来像

boolean allMatch = true;
for (String kw : keywords) {  // <-- for each kw in keywords
    if (!word.contains(kw)) { // <-- if "word" doesn't contain kw
        allMatch = false;     // <-- set allMatch to false
        break;                // <-- stop checking
    }
}
if (allMatch) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}

Note this solution work only if your keywords contain one char , there are many answers already mentioned if your keywords contain more then one char.请注意,此解决方案仅在您的关键字包含一个字符时才有效,如果您的关键字包含多个字符,则已经提到了许多答案。

With one line :一行:

boolean contain = Arrays.asList(word.split("")).containsAll(Arrays.asList(keywords));

The idea is :这个想法是:

String word = "abc";
String[] split = word.split("");

Split your String to get an array of chars split = {a, b, c}拆分你的字符串以获得一个字符数组split = {a, b, c}

String[] keywords = {"a", "b"};

Check if your array1 contain all the element of the second array2 using containsAll使用containsAll检查您的array1包含第二个array2所有元素

boolean contain = Arrays.asList(split).containsAll(Arrays.asList(keywords));

Either use StringUtils ( org.apache.commons.lang.StringUtils ).要么使用 StringUtils ( org.apache.commons.lang.StringUtils )。 It will return the index of the first occurrence of keywords or -1 if it is not there.它将返回第一次出现关键字的索引,如果不存在则返回 -1。

StringUtils.indexOfAny(word, keywords);

Or you can use Arrays which will return the boolean value.或者您可以使用将返回布尔值的Arrays

Arrays.asList(word).contains(keywords)

Simply use a counter:只需使用计数器:

String word = "abc";
String[] keywords = {"a", "d"};

int counter = 0;

for(int i = 0; i < keywords.length; i++){
    if(word.contains(keywords[i])){
       counter++;
    }
}

if(counter == keywords.length){
   System.out.println("Yes");
}else{
   System.out.println("No");   
}

If the counter equals the keywords length, it means that all elements are contained.如果计数器等于关键字长度,则表示包含所有元素。 With this solution you will also be able to find out how many keywords are matched by the word.使用此解决方案,您还可以找出与该词匹配的关键字数量。

A better code would be:更好的代码是:

for(String s: keywords){
    if(word.contains(s)){
       System.out.println("Yes");
    }else{
       System.out.println("No");   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM