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 valu开发者_如何学运维es. I dont want to have too many or conditions. what is a better way of doing it?
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.
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.
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
Create a static array of strings. Also java enum is a good idea.
精彩评论