Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #295 String to Enum

Given the enumerated type T, create a function TryStrToEnum that takes a string s as input and converts it into an enum value of type T.

Explain whether the conversion is case sensitive or not.
Explain what happens if the conversion fails.

T x = T.valueOf(s);

Case sensitive.

Throws java.lang.IllegalArgumentException in case of no match.
X : T := T'Value (S);

Case-insensitive conversion of trimmed string S to enumeration type T. Constraint_Error is raised otherwise.

New implementation...
< >
Bart