Given the following java enum:
public enum AgeRange { A18TO23 { public String toString() { return '18 - 23'; } }, A24TO29 { public String toString() { return '24 - 29'; } }, A30TO35 { public String toString() { return '30 - 35'; } }, }
Is there any way to convert a string value of ’18 – 23′ to the corresponding enum value i.e. AgeRange.A18TO23 ?
Thanks!
The best and simplest way to do it is like this:
Then you just need to invoke the
getByValue()method with theStringinput in it.