数据加载中……
how to do JAVA String switch

The thing I miss about Java is that it doesn't have string switch functionality like C#. The only way to get close to achieving this in Java is through enums.

A lot of people wonder how that can be done but when I read on a forum post about it I decided to do it myself.
Firstly you'll need the strings you want to look out for. Create an enum containing them, just know that it can only be 1 word strings, don't even try multiple words!

enum StrList
{
car,
dog,
human
}

Now we can take a whole string and pass it through the switch statement.

switch(StrList.valueOf(incommingStr.toLowerCase()))
{
case car:
//Do what you want with the 'car'
break;
case dog:
//Do what you want with the 'dog'
break;
case human:
//Do what you want with the 'human'
break;
}

posted on 2009-03-03 02:24 yellowstone 阅读(691) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航: