Arav
Arav

Reputation: 5247

Regarding String If Condition

I have a lot of static values "ABC" , "DEF" , "FGH" ,"IJK", "JKL" I want to have an if condition that checks whether the a String variable exists in this list of static values. I dont want to have too many or conditions. what is a better way of doing it?

Upvotes: 0

Views: 214

Answers (4)

punny
punny

Reputation: 403

You can create an ArryList as below:

ArrayList<String> arrList = new ArrayList<String>();
arrList.add("ABC");
arrList.add("DEF");
arrList.add("FGH");
arrList.add("IJK");
arrList.add("JKL");
System.out.println(arrList.contains("AAA"))
System.out.println(arrList.contains("FGH"));

The example result:

false
true

Upvotes: 1

twodayslate
twodayslate

Reputation: 2833

If you are using PHP you can use the in_array function. Just put all the static values in an array and see if the string is in the array using the function.

If you are using Java then I would recommend checking this thread out.

Upvotes: 2

jaywon
jaywon

Reputation: 8234

Not sure what language you're using, but you could put those values into an array, and then do a search on the array to see if the value you're matching exists anywhere in the array.

Upvotes: 1

fastcodejava
fastcodejava

Reputation: 41087

Create a static array of strings. Also java enum is a good idea.

Upvotes: 0

Related Questions