joe dacoolguy
joe dacoolguy

Reputation: 309

Checking if a string contains any of the strings in an array

Without for () loops such as

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

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.

I have tried

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

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

but this returns an error.

edit:

To clear it up:

I want to do something like

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

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.

Upvotes: 0

Views: 11113

Answers (4)

noclayto
noclayto

Reputation: 160

For loop:

for (String search : array) {
    if (message.contains(search)) {
        return true;
    }
}

return false;

Lambdas:

return Arrays.stream(array).anyMatch(message::contains);

Upvotes: 2

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62874

You can do it like this:

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

Upvotes: 3

Gaston Flores
Gaston Flores

Reputation: 2467

Try use lambdaj (download here,website) and hamcrest (download here,website), this libraries are very powerfull for managing collections, the following code is very simple and works perfectly:

import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.select;
import static org.hamcrest.Matchers.containsString;
import java.util.Arrays;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("A","BB","DCA","D","x");
        String strTofind = "C";
        System.out.println("List: " + list.toString());
        boolean match = select(list, having(on(String.class), containsString(strTofind))).size()>0;
        System.out.println("The string " + strTofind + (!match?" not":"") + " exists");

        strTofind = "X";
        match = select(list, having(on(String.class), containsString(strTofind))).size()>0;
        System.out.println("The string " + strTofind + (!match?" not":"") + " exists");        
    }
}

This shows:

List: [A, BB, DCA, D, x]
The string C exists
The string X not exists

Basically, in one line you can search the string is in any strinf of the array:

boolean match = select(list, having(on(String.class), containsString(strTofind))).size()>0;

With this libraries you can solve your problem in one line. You must add to your project: hamcrest-all-1.3.jar and lambdaj-2.4.jar Hope this will be useful.

Note: in my example i use a List then if you want use an array: Arrays.asList(array) (array is string[])

Upvotes: 2

GauravS
GauravS

Reputation: 91

Give this a try

String longString = "This is a string.";
String[] stringArray = new String[]{"apple", "ball", "This", "cat"};
int index = 0;
while(index <stringArray.length){
    if(longString.contains(stringArray[index])){
        System.out.println("found: "+stringArray[index]);
    }
    index++;
}

Upvotes: 0

Related Questions