How to compare string to enum type in Java?

后端 未结 3 631
无人共我
无人共我 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:27

    to compare enum to string

    for (Object s : State.values())
        {
                if (theString.equals(s.toString()))
            {
                // theString is equal State object
    
            }
        }
    

提交回复
热议问题