I know that apache-commons has a utility class called StringUtils that could give you a elegant solution.
public boolean foo(String[] array, String pattern){
for(String content : array){
if(StringUtils.contains(content, pattern){
return true;
}
}
return false;
}
One thing I don't like about this is that it will only return true at the first found instance. I'm not entirely sure what you are attempting to do but if you don't if don't care about indexes in the array that don't match the pattern, I would recommend using the higher order function called filter.
Guava, lambdaJ, and Apache-Commons, are libraries that have support for functional programming.
Below is some sudo-code that should work in Apache-Commons.
List<String> content = Arrays.asList(strArray);
Predicate matchesPattern = new Predicate("asdf"){{
private String pattern;
public Predicate(String pattern){
this.pattern = pattern;
}
@Overload
public boolean evaluate(Object input){
if(input instanceOf String){
StringUtils.contains((String)input, pattern
}
return false;
}
}};
CollectionUtils.filter(content, matchesPattern);
What this does is remove any String from the list that doesn't matches the pattern. As you can see it's a little verbose declaring a Predicate object. If you use Apache-Commons or Guava it's going to look similar, but that's where lambdaJ comes to the rescue.
A predicate is just term for function that takes in a single argument and returns a boolean value, you probably already used them before with the Matcher class. Hamcrest has some of the best Matcher's library available, so lambdaJ just built a functional programming library around it. It's easy to use and highly readable.