How to compare string to enum type in Java?

后端 未结 3 630
无人共我
无人共我 2021-01-11 18:36

I have an enum list of all the states in the US as following:

public enum State
{ AL, AK, AZ, AR, ..., WY }

and in my test file, I will rea

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-11 19:08

    Use .name() method. Like st.name(). e.g. State.AL.name() returns string "AL".

    So,

    if(st.name().equalsIgnoreCase(s)) {
    

    should work.

提交回复
热议问题