Java 有没有办法将开关与 String.contains("") 集成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21255198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a way to integrate a switch with String.contains("")?
提问by user840930
I need to check if a String object contains() various substrings and based on the results have different pieces of code execute. Currently I have a series of else if. I would like to convert it into a switch if possible. Is there a way to do this?
我需要检查 String 对象是否包含()各种子字符串,并根据结果执行不同的代码段。目前我有一系列 else if。如果可能,我想将其转换为开关。有没有办法做到这一点?
currently:
目前:
if (SomeString.contains("someSubString")) {
. . . do something
} else if (SomeString.contains("anotherSubString")) {
. . . do something else
} else if (SomeString.contains("yetanotherSubString")) {
. . . do something even more different
}
.
.
.
采纳答案by Silly Freak
When I have a situation like this, and really don't want to use else if
, I do something like this:
当我遇到这样的情况并且真的不想使用时else if
,我会这样做:
String[] cases = {"someSubString", "anotherSubString", "yetanotherSubString"};
int i;
for(i = 0; i < cases.length; i++)
if(SomeString.contains(cases[i])) break;
switch(i) {
case 0: //someSubString
System.out.println("do something");
break;
case 1: //anotherSubString
System.out.println("do something else");
break;
case 2: //yetanotherSubString
System.out.println("do something even more different");
break;
default:
System.out.println("do nothing");
}
Of course, that has a couple of downsides. For starters, if you add another strings anywhere but the end, all the indices will shift, and you have to correct for that manually. Still, I think I have used something like that once or twice and thought it makes the code more readable. Presumably, the indices had some meaning in the program, thus coupling the 0...someSubString
in a way meaningful to the problem.
当然,这有几个缺点。首先,如果您在除末尾之外的任何地方添加另一个字符串,所有索引都会发生变化,您必须手动进行更正。尽管如此,我认为我已经使用过一两次这样的东西,并认为它使代码更具可读性。据推测,索引在程序中具有某种意义,因此0...someSubString
以对问题有意义的方式耦合。
回答by Brierson
You can't use a String variable in a switch, but you can use a char, maybe a specific char is diferent in all your strings?
您不能在 switch 中使用 String 变量,但可以使用 char,也许特定的 char 在所有字符串中都不同?
For "someSubString", "anotherSubString" and "yetanotherSubString" you can use something like:
对于“someSubString”、“anotherSubString”和“yetanotherSubString”,您可以使用以下内容:
switch(SomeString.chatAt(0)) {
case 's':
case 'a':
case 'y':
}
But this is only valid if you know all possible values of the string and a character of a specific position is diferent on all of them.
但这仅在您知道字符串的所有可能值并且特定位置的字符在所有这些值上都不同时才有效。
回答by Tim B
In Java 7 you can switch on complete Strings, but not on partial skins. What you can do though is a strategy pattern to handle this sort of situation.
在 Java 7 中,您可以打开完整的字符串,但不能打开部分皮肤。不过,您可以做的是一种处理这种情况的策略模式。
private abstract class StringProcessor {
String pattern;
StringProcessor(String str) {
pattern = str;
}
boolean matches(String str) {
return str.matches(pattern);
}
abstract void process(String str);
}
StringProcessor[] processors = new StringProcessor[] {
new StringProcessor("patt1") {
void process(String str) {
// do stuff for patt1
}
},
new StringProcessor("patt2") {
void process(String str) {
// do stuff for patt2
}
}
};
Then in your method:
然后在你的方法中:
for (StringProcessor sp: processors) {
if (sp.matches(str)) {
sp.process(str);
break; // Don't break if you want the option to handle all that match
}
}
Now you can add as many different StringProcessors as you want and the will all get checked and if any match the code will be called. In many cases you can pre define the list of processors ahead of time but if need be you can create it dynamically too.
现在,您可以根据需要添加任意数量的不同 StringProcessor,并且所有这些都将被检查,如果有任何匹配,则将调用代码。在许多情况下,您可以提前预先定义处理器列表,但如果需要,您也可以动态创建它。
回答by Goutham Shivakumar Nandipati
For same type of situation i used as below. First got all string which i am looking into as a string
对于我使用的相同类型的情况,如下所示。首先得到我正在研究的所有字符串
String str ="someSubString,anotherSubString,yetanotherSubString"
String str ="someSubString,anotherSubString,yetanotherSubString"
Then get the list out of above string
然后从上面的字符串中取出列表
List items = Arrays.asList(str.split("\s*,\s*"));
List items = Arrays.asList(str.split("\s*,\s*"));
Now loop above list and use contain and switch
现在循环上面的列表并使用包含和切换
<pre>
for (String item : items) {
if (mainStr.contains(item)) {
switch (item) {
case "someSubString":
do something
break;
case "anotherSubString":
do something else
break;
case "yetanotherSubString":
do something even more different
break;
}
}
}
</pre>
Hope this will help guys who may be looking for same type of requirement now.
希望这会帮助那些现在可能正在寻找相同类型要求的人。