How can i check if a string matches String array

jp singh :

How can i check if a string matches any of elements in string array.

Suppose a String array contain {abcdef, ghijkl} Comparing it with a string "abc" should return the first value

If have been using stringArray.contains(string) method but it seems to work only if the complete string matches. I need to get the result even if the string partially matches

Any help would be appreciated

GhostCat salutes Monica C. :

Streams come in handy:

Arrays.stream(stringArray)
  .filter(s -> s.contains(stringToSearch))
  .findFirst();

Of course, your requirements boil down to: find the first string in the array that starts with the search string. But actually your requirements are a bit unclear, so you should apply the filter as required. There is a big difference between contains() and startsWith() for example!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=128187&siteId=1