在 Java 中搜索字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2834270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:25:03 来源:igfitidea点击:
Search a String Array in Java?
提问by Praveen
How do I search a string array with start with keyword?
如何搜索以关键字开头的字符串数组?
for example,
例如,
String[] str = { "abcd", "abdc", "bcda"};
when my search string is "a"
it must show
当我的搜索字符串是"a"
它必须显示
abcd and abdc
when my search string is "abc"
then it should be "abcd"
.
当我的搜索字符串是"abc"
那么它应该是"abcd"
.
采纳答案by aioobe
String[] strArray = {"abcd", "abdc", "bcda"};
for (String s : strArray)
if (s.startsWith(searchTerm))
System.out.println(s);
Swap startsWith
for contains
you wish to simply look for containment.
交换startsWith
为contains
您希望简单地寻找遏制。