本文介绍了在Java中搜索字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何搜索以关键字开头的字符串数组?
How do I search a string array with start with keyword?
例如,
String[] str = { "abcd", "abdc", "bcda"};
当我的搜索字符串是"a"
时,它必须显示
when my search string is "a"
it must show
abcd and abdc
当我的搜索字符串为"abc"
时,则应为"abcd"
.
when my search string is "abc"
then it should be "abcd"
.
推荐答案
String[] strArray = {"abcd", "abdc", "bcda"};
for (String s : strArray)
if (s.startsWith(searchTerm))
System.out.println(s);
将 startsWith
替换为 contains
,您只希望查找收容条件.
Swap startsWith
for contains
you wish to simply look for containment.
这篇关于在Java中搜索字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!