http://stackoverflow.com/questions/604424/java-convert-string-to-enum
/**
* A common method for all enums since they can't have another base class
* @param <T> Enum type
* @param c enum type. All enums must be all caps.
* @param string case insensitive
* @return corresponding enum, or null
*/
public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string)
{
if( c != null && string != null )
{
try
{
return Enum.valueOf(c, string.trim().toUpperCase());
}
catch(IllegalArgumentException ex)
{
}
}
return null;
}
本文介绍了一个通用的方法,用于将字符串转换为对应的枚举类型。此方法适用于所有枚举,并且能够处理大小写不敏感的情况。
1221

被折叠的 条评论
为什么被折叠?



