我正在处理一个Switch语句,并且我需要查看两个条件,看看值是否以特定值开头。 Switch语句是这样的。错误说“不能将bool类型转换为字符串”。
任何人都知道我是否可以在Switch中使用StartsWith或者我是否需要使用If ... Else语句?
switch(subArea)
{
case "4100":
case "4101":
case "4102":
case "4200":
return "ABC";
case "600A":
return "XWZ";
case subArea.StartsWith("3*"):
case subArea.StartsWith("03*"):
return "123";
default:
return "ABCXYZ123";
}
答案 0 :(得分:12)
您正在切换String
,而subArea.StartsWith()
会返回Boolean
,这就是您无法做到的原因。我建议你这样做:
if (subArea.StartsWith("3*") || subArea.StartsWith("03*"))
return "123";
switch(subArea)
{
case "4100":
case "4101":
case "4102":
case "4200":
return "ABC";
case "600A":
return "XWZ";
default:
return "ABCXYZ123";
}
结果将是相同的。
答案 1 :(得分:5)
case标签必须是字符串,因为switch表达式是一个字符串;但是,StartsWith
返回一个布尔值。我建议在default
部分处理这些特殊情况。
switch(subArea)
{
case "4100":
case "4101":
case "4102":
case "4200":
return "ABC";
case "600A":
return "XWZ";
default:
if (subArea.StartsWith("3") || subArea.StartsWith("03")) {
return "123";
}
return "ABCXYZ123";
}
除非您希望subArea
包含它,否则明星(*)可能是错误的。 StartWith
不接受通配符。
或者你可以使用正则表达式:
if (Regex.IsMatch(subArea, "^3|^03")) { // or "^(3|03)"
return "123";
}
其中^
表示行首,|
表示或。
答案 2 :(得分:4)
Joe有点打败我,但这是另一种非切换方式,它基本上实现了一个带有规则集的模式匹配算法。
private static string GetSomeStringOrOther(string subArea)
{
// Create a set of pattern matching functions...
Func<string, string, bool> matchEquals = (a, b) => a.Equals(b);
Func<string, string, bool> matchStarts = (a, b) => a.StartsWith(b);
// Create a rule set...
Tuple<string, string, Func<string, string, bool>>[] cases = new []
{
new Tuple<string, string, Func<string, string, bool>>("4100", "ABC", matchEquals),
new Tuple<string, string, Func<string, string, bool>>("4101", "ABC", matchEquals),
new Tuple<string, string, Func<string, string, bool>>("4102", "ABC", matchEquals),
new Tuple<string, string, Func<string, string, bool>>("4200", "ABC", matchEquals),
new Tuple<string, string, Func<string, string, bool>>("600A", "XWZ", matchEquals),
new Tuple<string, string, Func<string, string, bool>>("3*", "123", matchStarts),
new Tuple<string, string, Func<string, string, bool>>("03*", "123", matchStarts),
};
// Look for a match...
foreach(var matchCase in cases)
{
if(matchCase.Item3(subArea, matchCase.Item1))
{
// Return if it matches...
return matchCase.Item2;
}
}
// Otherwise return the default...
return "ABCXYZ123";
}
<强>优点强>
<强>缺点强>
<强>改进强>
Tuple<string, string, Func<string, string, bool>>
替换为代表Rule
答案 3 :(得分:4)
自C#7起,您可以执行以下操作:
switch(subArea)
{
case "4100":
case "4101":
case "4102":
case "4200":
return "ABC";
case "600A":
return "XWZ";
case string s when s.StartsWith("3*"):
return "123";
case string s when s.StartsWith("03*"):
return "123";
default:
return "ABCXYZ123";
}
答案 4 :(得分:3)
使用LINQ,● hadoop-datanode.service - Hadoop Datanode
Loaded: loaded (/usr/lib/systemd/system/hadoop-datanode.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-12-29 05:29:15 JST; 53s ago
Process: 30450 ExecStart=/usr/bin/hadoop datanode (code=exited, status=1/FAILURE)
Main PID: 30450 (code=exited, status=1/FAILURE)
Dec 29 05:29:14 <my user name> hadoop[30450]: at org.apache.hadoop.hdfs.server.datanode.DataNode.createDataNode(DataNode.java:2728)
Dec 29 05:29:14 <my user name> hadoop[30450]: at org.apache.hadoop.hdfs.server.datanode.DataNode.secureMain(DataNode.java:2872)
Dec 29 05:29:14 <my user name> hadoop[30450]: at org.apache.hadoop.hdfs.server.datanode.DataNode.main(DataNode.java:2896)
Dec 29 05:29:14 <my user name> hadoop[30450]: 2017-12-29 05:29:14,859 INFO util.ExitUtil: Exiting with status 1: java.io.IOException: Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured.
Dec 29 05:29:14 <my user name> hadoop[30450]: 2017-12-29 05:29:14,865 INFO datanode.DataNode: SHUTDOWN_MSG:
Dec 29 05:29:14 <my user name> hadoop[30450]:
/************************************************************
Dec 29 05:29:14 <my user name> hadoop[30450]: SHUTDOWN_MSG: Shutting down DataNode at elect/10.0.2.15
Dec 29 05:29:14 <my user name> hadoop[30450]:
************************************************************/
Dec 29 05:29:15 elect systemd[1]: hadoop-datanode.service: Main process exited, code=exited, status=1/FAILURE
Dec 29 05:29:15 <my user name> systemd[1]: hadoop-datanode.service: Failed with result 'exit-code'.
● hadoop-namenode.service - Hadoop Namenode
Loaded: loaded (/usr/lib/systemd/system/hadoop-namenode.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-12-29 05:29:13 JST; 10min ago
Process: 30449 ExecStart=/usr/bin/hadoop namenode (code=exited, status=1/FAILURE)
Main PID: 30449 (code=exited, status=1/FAILURE)
Dec 29 05:29:13 <my user name> hadoop[30449]: at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(NameNode.java:929)
Dec 29 05:29:13 <my user name> hadoop[30449]: at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1653)
Dec 29 05:29:13 <my user name> hadoop[30449]: at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1720)
Dec 29 05:29:13 <my user name> hadoop[30449]: 2017-12-29 05:29:13,366 INFO util.ExitUtil: Exiting with status 1: java.lang.IllegalArgumentException: Invalid URI for NameNode address (check fs.defaultFS): file:/// has no authority.
Dec 29 05:29:13 <my user name> hadoop[30449]: 2017-12-29 05:29:13,368 INFO namenode.NameNode: SHUTDOWN_MSG:
Dec 29 05:29:13 <my user name> hadoop[30449]:
/************************************************************
Dec 29 05:29:13 <my user name> hadoop[30449]: SHUTDOWN_MSG: Shutting down NameNode at <my user name>/10.0.2.15
Dec 29 05:29:13 <my user name> hadoop[30449]:
************************************************************/
Dec 29 05:29:13 <my user name> systemd[1]: hadoop-namenode.service: Main process exited, code=exited, status=1/FAILURE
Dec 29 05:29:13 <my user name> systemd[1]: hadoop-namenode.service: Failed with result 'exit-code'.
● hadoop-jobtracker.service - Hadoop Jobtracker
Loaded: loaded (/usr/lib/systemd/system/hadoop-jobtracker.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-12-29 05:26:04 JST; 17min ago
Process: 29726 ExecStart=/usr/bin/hadoop jobtracker (code=exited, status=1/FAILURE)
Main PID: 29726 (code=exited, status=1/FAILURE)
Dec 29 05:26:04 <my user name> hadoop[29726]: queue get information regarding JobQueues
Dec 29 05:26:04 <my user name> hadoop[29726]: sampler sampler
Dec 29 05:26:04 <my user name> hadoop[29726]: streaming launch a mapreduce streaming job
Dec 29 05:26:04 <my user name> hadoop[29726]: version print the version
Dec 29 05:26:04 <my user name> hadoop[29726]: Daemon Commands:
Dec 29 05:26:04 <my user name> hadoop[29726]: tput: No value for $TERM and no -T specified
Dec 29 05:26:04 <my user name> systemd[1]: hadoop-jobtracker.service: Main process exited, code=exited, status=1/FAILURE
Dec 29 05:26:04 <my user name> hadoop[29726]: historyserver run job history servers as a standalone daemon
Dec 29 05:26:04 <my user name> hadoop[29726]: SUBCOMMAND may print help when invoked w/o parameters or with -h.
Dec 29 05:26:04 <my user name> systemd[1]: hadoop-jobtracker.service: Failed with result 'exit-code'.
● hadoop-secondarynamenode.service - Hadoop Secondary Namenode
Loaded: loaded (/usr/lib/systemd/system/hadoop-secondarynamenode.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-12-29 05:25:59 JST; 20min ago
Process: 29433 ExecStart=/usr/bin/hadoop secondarynamenode (code=exited, status=1/FAILURE)
Main PID: 29433 (code=exited, status=1/FAILURE)
Dec 29 05:25:59 <my user name> hadoop[29433]: at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.initialize(SecondaryNameNode.java:231)
Dec 29 05:25:59 <my user name> hadoop[29433]: at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.<init>(SecondaryNameNode.java:194)
Dec 29 05:25:59 <my user name> hadoop[29433]: at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.main(SecondaryNameNode.java:690)
Dec 29 05:25:59 <my user name> hadoop[29433]: 2017-12-29 05:25:59,387 INFO util.ExitUtil: Exiting with status 1: ExitException
Dec 29 05:25:59 <my user name> hadoop[29433]: 2017-12-29 05:25:59,400 INFO namenode.SecondaryNameNode: SHUTDOWN_MSG:
Dec 29 05:25:59 <my user name> hadoop[29433]:
/************************************************************
Dec 29 05:25:59 <my user name> hadoop[29433]: SHUTDOWN_MSG: Shutting down
SecondaryNameNode at <my user name>/10.0.2.15
Dec 29 05:25:59 <my user name> hadoop[29433]:
************************************************************/
Dec 29 05:25:59 <my user name> systemd[1]: hadoop-secondarynamenode.service: Main process exited, code=exited, status=1/FAILURE
Dec 29 05:25:59 <my user name> systemd[1]: hadoop-secondarynamenode.service: Failed with result 'exit-code'.
● hadoop-tasktracker.service - Hadoop Tasktracker
Loaded: loaded (/usr/lib/systemd/system/hadoop-tasktracker.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-12-29 05:26:04 JST; 23min ago
Process: 29727 ExecStart=/usr/bin/hadoop tasktracker (code=exited, status=1/FAILURE)
Main PID: 29727 (code=exited, status=1/FAILURE)
Dec 29 05:26:04 <my user name> hadoop[29727]: queue get information regarding JobQueues
Dec 29 05:26:04 <my user name> hadoop[29727]: sampler sampler
Dec 29 05:26:04 <my user name> hadoop[29727]: streaming launch a mapreduce streaming job
Dec 29 05:26:04 <my user name> hadoop[29727]: version print the version
Dec 29 05:26:04 <my user name> hadoop[29727]: Daemon Commands:
Dec 29 05:26:04 <my user name> hadoop[29727]: tput: No value for $TERM and no -T specified
Dec 29 05:26:04 <my user name> hadoop[29727]: historyserver run job history servers as a standalone daemon
Dec 29 05:26:04 <my user name> hadoop[29727]: SUBCOMMAND may print help when invoked w/o parameters or with -h.
Dec 29 05:26:04 <my user name> systemd[1]: hadoop-tasktracker.service: Main process exited, code=exited, status=1/FAILURE
Dec 29 05:26:04 <my user name> systemd[1]: hadoop-tasktracker.service: Failed with result 'exit-code'.
可以通过将foreach
和return
语句替换为:{/ p>来简化the nice answer by @seriesOne
// using System.Linq;
// Look for a match...
var result = cases
.Where(c => c.Item3(subArea, c.Item1))
.FirstOrDefault();
// Return the match or the default.
return result == null ? "ABCXYZ123" : result.Item2;
答案 5 :(得分:2)
只是为了好玩,这是另一个避免切换声明的解决方案。
var map = new[] {
new { Value = "4100", StartsWith = false, Result="ABC" },
new { Value = "4101", StartsWith = false, Result="ABC" },
new { Value = "4102", StartsWith = false, Result="ABC" },
new { Value = "4200", StartsWith = false, Result="ABC" },
new { Value = "600A", StartsWith = false, Result="XWZ" },
new { Value = "3*", StartsWith = true, Result="123" },
new { Value = "03*", StartsWith = true, Result="123" },
};
var subarea = ... whatever ...;
var result = map.Where(e =>
{
if (e.StartsWith)
{
return subarea.StartsWith(e.Value);
}
else
{
return subarea == e.Value;
}
}
)
.Select(e => e.Result)
.FirstOrDefault() ?? "ABCXZ123";
数组map
中的顺序决定了优先级,因此,例如,您可以在&#34; 3 * 11&#34;以及{{{ 1}}匹配&#34; 3 *&#34;,例如:
StartsWith
答案 6 :(得分:2)
借助when clause,您现在可以执行以下操作:
Run 1:MAPE=20.99
Run 2:MAPE=30.02
Run 3:MAPE=50.22
答案 7 :(得分:2)
从 C# 9 开始,您可以执行以下操作:
switch(subArea)
{
case "4100":
case "4101":
case "4102":
case "4200":
return "ABC";
case "600A":
return "XWZ";
case { } when subArea.StartsWith("3*"):
case { } when subArea.StartsWith("03*"):
return "123";
default:
return "ABCXYZ123";
}