Friday, July 5, 2019

Java - search a string in string array




In java do we have any method to find that a particular string is part of string array.
I can do in a loop which I would like to avoid.



e.g.



String [] array = {"AA","BB","CC" };
string x = "BB"


I would like a



if (some condition to tell whether x is part of array) {
do something
} else {
do soemthing
}

Answer



Do something like:



Arrays.asList(array).contains(x);


since that return true if the String x is present in the array (now converted into a list...)





if(Arrays.asList(array).contains(x)){
// is present ... :)
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...