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 832f066

Browse filesBrowse files
committed
Merge pull request giantray#23 from yannxia/master
增加 convert-a-string-to-an-enum-in-java 问题翻译
2 parents 1000ff2 + 953f7ec commit 832f066
Copy full SHA for 832f066

File tree

Expand file treeCollapse file tree

2 files changed

+51
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+51
-1
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ stackoverflow-Java-top-qa
3131
* [wait()和sleep()的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md)
3232
* [能否在一个构造器( `constructor` )中调用另一个构造器](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-do-i-call-one-constructor-from-another-in-java.md)
3333
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
34+
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
3435

3536
> 编程技巧
3637
@@ -65,7 +66,6 @@ stackoverflow-Java-top-qa
6566
- [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed)
6667
- [Dealing with “java.lang.OutOfMemoryError: PermGen space” error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
6768
- [“implements Runnable” vs. “extends Thread”](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
68-
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
6969
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
7070
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
7171
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
##如何将String转换为enum
2+
3+
### 问题
4+
一个枚举定义:
5+
6+
```java
7+
public enum Blah {
8+
A, B, C, D
9+
}
10+
```
11+
并且我知道枚举的String值,比如 "A",我想将其转换为Blah.A,我应该怎么做?
12+
是否有Enum.valueOf() 这样的方法,如果是,那我如何使用?
13+
14+
15+
### 答案
16+
是的,Blah.valueOf("A") 将会得到 Blah.A
17+
18+
静态方法valueOf() 和 values() 会在编译期创建,不过这不会体现在源代码内,他们出现在JavaDoc中,比如 [Dialog.ModalityTyp](http://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.ModalityType.html) 中出现这两个方法。
19+
20+
### 其他答案
21+
22+
我有一个友善的工具方法:
23+
```java
24+
/**
25+
* A common method for all enums since they can't have another base class
26+
* @param <T> Enum type
27+
* @param c enum type. All enums must be all caps.
28+
* @param string case insensitive
29+
* @return corresponding enum, or null
30+
*/
31+
public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
32+
if( c != null && string != null ) {
33+
try {
34+
return Enum.valueOf(c, string.trim().toUpperCase());
35+
} catch(IllegalArgumentException ex) {
36+
}
37+
}
38+
return null;
39+
}
40+
```
41+
42+
你可以这么使用:
43+
44+
```java
45+
public static MyEnum fromString(String name) {
46+
return getEnumFromString(MyEnum.class, name);
47+
}
48+
```
49+
50+
stackoverflow链接:http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java

0 commit comments

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