通过字符串值查找枚举 [英] Lookup enum by string value

查看:479
本文介绍了通过字符串值查找枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个枚举只是

  public enum Blah {
A,B,C,D
}

我想找到字符串的枚举值,例如A这将是 Blah.A 。这样做可能如何?



我需要的方法是$ code Enum.valueOf()如果是这样,我该如何使用?

解决方案

是, Blah.valueOf(A) 将给你 Blah.A



静态方法 valueOf() values()在编译时创建,不会出现在源代码中。他们确实出现在Javadoc;例如, 对话框。 ModalityType 显示两种方法。


Say I have an enum which is just

public enum Blah {
    A, B, C, D
}

and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to do this?

Is the Enum.valueOf() the method I need? If so, how would I use this?

解决方案

Yes, Blah.valueOf("A") will give you Blah.A.

The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods.

这篇关于通过字符串值查找枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆