Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c223730

Browse filesBrowse files
committed
Create onvert-a-string-to-an-enum-in-java.md
1 parent 2cc5875 commit c223730
Copy full SHA for c223730

File tree

Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-0
lines changed
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
```java
17+
Blah A = Blah.valueOf("A");
18+
```
19+
这样传入"A"会返回Balsh枚举类.
20+
###回答2
21+
```java
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.