We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2cc5875 commit c223730Copy full SHA for c223730
contents/onvert-a-string-to-an-enum-in-java.md
@@ -0,0 +1,30 @@
1
+##在java中把String转换给enum类
2
+-------------------------------------
3
+
4
+###问题
5
+假设有有个枚举类:
6
+```java
7
+public enum Blah
8
+{
9
+ A, B, C, D
10
+}
11
+```
12
+现在我想把这个String转成枚举类,比如说"A"应该等于Blash.A.该怎么做?
13
14
+------
15
+###回答1
16
17
+Blah A = Blah.valueOf("A");
18
19
+这样传入"A"会返回Balsh枚举类.
20
+###回答2
21
22
+Blah A =Enum.valueOf(Blah.class, "A");
23
24
+同样可以得到该枚举类
25
26
+**这两个方法都会传入的参数大小写敏感,这个例子如果传入"a",则会报错No enum const class Blah.a.**
27
28
+stackoverflow原址: http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java
29
30
0 commit comments