开发者

Java - Search using a part of key word

I have requirement of selecting s subset of data from a collection using a part of a key word

Assume that I have a collection consist of following entries,

"Pine Grosbeak"  
"House Crow"  
"Hume`s Leaf Warbler"  
"Great Northern Loon"  
"Long-tailed Duck"  
"Lapland Longspur"  
"Northern Gannet"  
"Eastern Imperial Eagle开发者_开发知识库"  
"Little Auk"  
"Lesser Spotted Woodpecker"  
"Iceland Gull" 

When I provide a search string as “ro” following should be filtered;

"House Crow"  
"Pine Grosbeak"  

(This is something similar to “LIKE ‘%ro%’ “ clause in SQL)

Can some one help me on this?


You could simply do something like that:

for (String s : strings)
{
    if (s.contains(search))
    {
        // match
    }
}

And if you want to be case insensitive:

String lowerSearch = search.toLowerCase();  
for (String s : strings)
{
    if (s.toLowerCase().contains(lowerSearch))
    {
        // match
    }
}


Iterate through and use contains() method to filter

List<String> lst = new ArrayList<String>();  
lst.add("Pine Grosbeak");
lst.add("House Crow");
.
.
.
for(String str:lst){  
if(str.contains(keyword)){
System.out.println("matched : "+ str);
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜