检查字符串是否包含数组中的任何字符串 [英] Checking if a string contains any of the strings in an array

查看:257
本文介绍了检查字符串是否包含数组中的任何字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有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);

这篇关于检查字符串是否包含数组中的任何字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆