In Java, I want to check whether a String exists in a List
.
Something like this:
if(myList.contains(\"A\")){
//
You can do it in a single line by using regex:
if (myList.toString().matches(".*\\bA\\b.*"))
This code should perform quite well.
BTW, you could build the regex from a variable, like this:
.matches("\\[.*\\b" + word + "\\b.*]")
I added [
and ]
to each end to prevent a false positive match when the search term contains an open/close square bracket at the start/end.