我有这段代码。当时并不明显,但由于“fc”和“fcip”都以“fc”开头,因此写入的代码总是会选择第一个选项。String StartsWith()任何避免2次检查的方法?
string fcportdelimit = "fc";
string fcipportdelimit = "fcip";
if (BlockToProcess[0].StartsWith(fcportdelimit))
{
try
{
this.ParseFCInterface(BlockToProcess);
}
catch (Exception E)
{
throw;
}
}
else if (BlockToProcess[0].StartsWith(fcipportdelimit))
{
try
{
this.ParseFCIPInterface(BlockToProcess);
}
catch (Exception E)
{
throw;
}
}
我查看了字符串类,但没有看到将模式作为输入的StartsWith()或Contains()。我正在测试的字符串要么是一个patttern fcN/N,要么是fcipN,其中N是一个数字。所以,我想我必须做这样的事情?
if (BlockToProcess[0].StartsWith(fcportdelimit || fcipportdelimit)
{
if (BlockToProcess[0].StartsWith(fcipportdelimit)
{
// do something here
}
else
{
//since fcipportdelimit didn't match it must be an fcport
//so do something else
}
}
'StartsWith(fcportdelimit || fcipportdelimit)'是有效的语法很远...... – 2012-03-06 17:57:17
听起来像是正是这类问题该正则表达式的是。也许看看他们。 – 2012-03-06 17:59:10
你考虑过正则表达式匹配吗? – Brettski 2012-03-06 17:59:42