Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a String Array in Java?

Tags:

java

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

abcd and abdc

when my search string is "abc" then it should be "abcd".

like image 206
Praveen Avatar asked May 09 '26 02:05

Praveen


1 Answers

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.

like image 96
aioobe Avatar answered May 11 '26 16:05

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!