0

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.

joe dacoolguy
  • 309
  • 2
  • 8
  • 18
  • 4
    Loops will have to be used somewhere. You can use `while` but not `for`? – Sotirios Delimanolis Jul 17 '13 at 15:06
  • 5
    What exactly do you mean by "globally" here? Your question is very unclear. – Jon Skeet Jul 17 '13 at 15:07
  • 1
    Thanks for the very constructive&helping answer Luiggi. – joe dacoolguy Jul 17 '13 at 15:13
  • 1
    @joe dacoolguy, Luiggi Mendoza is right, there is no reason that a for loop would not work. Also, if that is all the code the while loop is doing, then that should instead be `arraylength = array.length()` **Edit**: By instead be I mean that instead of having a while loop just assign `arraylength` the value it would have at the end of the while loop – asimes Jul 17 '13 at 15:15

4 Answers4

3

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);
fdermishin
  • 3,519
  • 3
  • 24
  • 45
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

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[])

Gaston Flores
  • 2,457
  • 3
  • 23
  • 42
2

For loop:

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

return false;

Lambdas:

return Arrays.stream(array).anyMatch(message::contains);
noclayto
  • 160
  • 1
  • 6
0

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++;
}
GauravS
  • 81
  • 1
  • 6