Praveen
Praveen

Reputation: 91205

Search a String Array in 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".

Upvotes: 3

Views: 22289

Answers (2)

aioobe
aioobe

Reputation: 421310

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.

Upvotes: 8

Related Questions