字符串匹配问题

时间:2014-04-10 10:36:17

标签: string c#-4.0

我正在使用此代码:

 string[] spl = { };
 if (theLine.Contains("Value of perquisites u/s 17(2)"))
 spl = new string[] { "17(2)" };
 else if (theLine.Contains("Value of perquisites under section 17(2) (as per"))
 spl = new string[] { "(as per" };
 else if (theLine.Contains("Value of perquisites under section 17(2) (as per Form"))
 spl = new string[] { "Form" };
 else if (theLine.Contains("Value of perquisites u/s 17(2) (as per Form No.12BA,"))
 spl = new string[] { "12BA," };
 else if (theLine.Contains("Value of perquisites under section 17(2) (as per Form No.      12BA, wherever"))
 spl = new string[] { "wherever" };

当我搜索最后一个条件时,它将匹配第一个条件。 Plz提前帮助Thanx。

1 个答案:

答案 0 :(得分:0)

重新排列条件,以便首先匹配较长的字符串:

string[] spl = { };
if (theLine.Contains("Value of perquisites under section 17(2) (as per Form No.      12BA, wherever"))
  spl = new string[] { "wherever" }
else if (theLine.Contains("Value of perquisites u/s 17(2) (as per Form No.12BA,"))
  spl = new string[] { "12BA," };
else if (theLine.Contains("Value of perquisites u/s 17(2)"))
  spl = new string[] { "17(2)" };
else if (theLine.Contains("Value of perquisites under section 17(2) (as per Form"))
  spl = new string[] { "Form" };
else if (theLine.Contains("Value of perquisites under section 17(2) (as per"))
  spl = new string[] { "(as per" };

这也将使(先前)第三和第四选项可用。