检查字符串是否包含数组中的任何字符串

问题描述:

没有for()循环,例如

Without for () loops such as

 for(int j =0; j < array.length; j++)
                {
}

我希望能够检查字符串是否包含全局数组中的任何字符串。所以for()循环不起作用。

I want to be able to check if a string contains any of the strings in an array globally. So for() loops don't work.

我已经尝试过

int arraylength;
while(arraylength < array.length()){
arraylength++; }

if(string.contains(array[arraylength]) {}

但这会返回错误。

编辑:

要清除它:

我想做类似的事情

if (string.contains(**code that checks all strings in an array**)

所以我可以检查string是否包含如前所述,for循环不起作用,因为我希望能够执行该类中ANYWHERE上方的代码行。

So I can check if string contains any of the strings in an array. As I have mentioned, for loops DO NOT work because I want to be able to execute the line of code above ANYWHERE in the class.

您可以这样:

String veryHugeString = ....;//
String[] words = new String[]{...};
boolean foundAtLeastOne = false;
for (String word : words) {
   if (veryHugeString.indexOf(word) > 0) {
       foundAtLeastOne = true;
       System.out.println("Word: " + word + " is found);
       break;
   }
}

System.out.println("Found at least one : " + foundAtLeastOne);