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

Sign up
Appearance settings

Commit 36e774a

Browse files
committed
Merge pull request giantray#44 from tangculijier/master
Create onvert-a-string-to-an-enum-in-java.md
2 parents 016ec72 + c223730 commit 36e774a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines 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)

AltStyle によって変換されたページ (->オリジナル) /