1

how would you simplify all these conditions?

String s = "The wold is big"

if(s.contains("is") || s.contains("are") || s.contains("was") || s.contains("were")) 
{ 
    return s;
}

Since I have to check several cases like those, is there a way to simplify all those conditions?

Josh
  • 63
  • 1
  • 5

4 Answers4

4

I would write a utility method for that:

public static boolean containsAny(String s, String... words) {
    for (String word : words) {
        if (s.contains(word))
            return true;
    }
    return false;
}

And now:

if (containsAny(s, "is", "are", "was", "were")) {...}

Java 8 alternative:

if (Arrays.asList("is", "are", "was", "were").stream().anyMatch(s::contains)) {...}
arshajii
  • 127,459
  • 24
  • 238
  • 287
3

You can use regex for this using the matches method of the String

sample:

String s = "The wold is big";

    if (s.matches("(.*)(is|are|was|were)(.*)")) {
        System.out.println("lawl");
    }
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Can't the regex be simplified to `"is|are|was|were"` – clcto Jul 29 '14 at 22:01
  • I just want to add that this will have to iterate over entire string, even if it will find match for `is` `are` `was` `were`. Original solution (or [arshajii approach](http://stackoverflow.com/a/25025632/1393766)) is faster. – Pshemo Jul 29 '14 at 22:03
  • @Pshemo he just want to simplify it with lesser code. – Rod_Algonquin Jul 29 '14 at 22:05
  • I am not saying your answer is incorrect (I am also not the downvoter), just pointing out that this is trade: less code for potentially worse performance (in case of long texts). – Pshemo Jul 29 '14 at 22:07
  • @Pshemo if `were` was the word in the file, then arshajii's approach has to go through the text 3+ times, while this only goes through it once. – clcto Jul 29 '14 at 22:09
  • @clcto OK, you are kind of right. Before first `(.*)` will be able to consume each characters which is not part of `is|are|was|were` it will have to compare them with each one of this words so we still have 4 comparisons. But yes, when we find match (regardless of which word was it) rest of string will be read only once. So both answers have its pros and cons. If we would like to improve it farther we could use instead of `matches` `find` from `Matcher`. This way we would not have to iterate till end of String. – Pshemo Jul 29 '14 at 22:17
0

There is not realy a quicker way to do it. Only if you have a lot of them it would be cleaner to put them in an array and walk over the array.

Also answered here: Test if a string contains any of the strings from an array

Community
  • 1
  • 1
Vennik
  • 565
  • 3
  • 11
0

You can pass an array of value to check :

public boolean Contains(String[] list, String elem) {
    for (String s: list){
        if(elem.contains(s)) {
            return true;
        }
    }
    return false;
}

String s = "The wold is big"
String[] conditions = {"is","are","was","were"}; 

if(Contains(conditions,s)) 
{ 
    return s;
}
Thom-x
  • 841
  • 1
  • 6
  • 20