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.

asked Jul 17, 2013 at 15:06
4
  • 4
    Loops will have to be used somewhere. You can use while but not for? Commented Jul 17, 2013 at 15:06
  • 5
    What exactly do you mean by "globally" here? Your question is very unclear. Commented Jul 17, 2013 at 15:07
  • 1
    Thanks for the very constructive&helping answer Luiggi. Commented Jul 17, 2013 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 Commented Jul 17, 2013 at 15:15

4 Answers 4

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,7443 gold badges27 silver badges50 bronze badges
answered Jul 17, 2013 at 15:07
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[])

answered Jul 17, 2013 at 16:19
2

For loop:

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

Lambdas:

return Arrays.stream(array).anyMatch(message::contains);
answered Dec 3, 2015 at 20:54
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++;
}
answered Jul 17, 2013 at 15:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.