[英]Most efficient way to search String array for substring
Say I have an array of Strings like so: 假设我有一个字符串数组,如下所示:
0 ["Some plain text"]
1 ["Foobar chicken"]
I want to search each String (in each index of the array) for a particular substring, say plain
, and then return true
when the first instance of the substring is found. 我想在每个String(在数组的每个索引中)搜索特定的子字符串,比如说plain
,然后在找到子字符串的第一个实例时返回true
。
What's the most effecient way to do this? 这样做最有效的方法是什么?
I know I can do a simple break
in a for-loop but I've heard people say that using break
in a for-loop is bad-practice. 我知道我可以在for循环中做一个简单的break
,但我听说有人说在for循环中使用break
是不好的做法。 I also hear that using a while
and do-while
isn't good either. 我也听说过,使用一段while
并且do-while
也不好。
Here's my simple implimentation using break: 这是我使用break的简单实现:
for (String[] index : tmpList) {
retVal = index[2].toLowerCase().contains(keyword);
if (retVal) // Break when retVal is true
break;
}
Where: 哪里:
tmpList
is an ArrayList<String[]>
tmpList
是一个ArrayList<String[]>
keyword
is what I'm trying to find keyword
是我想要找到的 I know I can do a simple break in a for-loop but I've heard people say that using break in a for-loop is bad-practice. 我知道我可以在for循环中做一个简单的休息,但我听说有人说在for循环中使用break是不好的做法。
Where did you find this? 你在哪里找到这个? That is completely wrong. 那是完全错误的。 Is it a bad practice to use break in a for loop? 在for循环中使用break是不好的做法吗?
Just use a for loop and loop through the String
s. 只需使用for循环并循环遍历String
。 Use String#contains
to check to see if the String
has a specific substring. 使用String#contains
检查String
是否具有特定的子字符串。 Then store the String
in a variable (or the index if you need it) and break;
然后将String
存储在变量(或索引,如果需要)中并break;
. 。
I know that apache-commons has a utility class called StringUtils that could give you a elegant solution. 我知道apache-commons有一个名为StringUtils的实用程序类,它可以为您提供一个优雅的解决方案。
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. 我不喜欢这件事的一件事是它只会在第一个找到的实例中返回true。 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. 我不完全确定你要做什么,但如果不这样做,如果不关心数组中与模式不匹配的索引,我建议使用名为filter的高阶函数。
Guava, lambdaJ, and Apache-Commons, are libraries that have support for functional programming. Guava,lambdaJ和Apache-Commons是支持函数式编程的库。
Below is some sudo-code that should work in Apache-Commons. 下面是一些应该在Apache-Commons中运行的sudo-code。
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. 这样做是从列表中删除与模式不匹配的任何String。 As you can see it's a little verbose declaring a Predicate object. 正如你所看到的,它是一个有点冗长的声明一个Predicate对象。 If you use Apache-Commons or Guava it's going to look similar, but that's where lambdaJ comes to the rescue. 如果你使用Apache-Commons或Guava,它看起来会很相似,但这就是lambdaJ拯救的地方。
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. 谓词就是函数的术语,它接受一个参数并返回一个布尔值,你可能以前在Matcher类中使用过它们。 Hamcrest has some of the best Matcher's library available, so lambdaJ just built a functional programming library around it. Hamcrest有一些最好的Matcher库,所以lambdaJ只是围绕它构建了一个函数式编程库。 It's easy to use and highly readable. 它易于使用且具有高可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.