开发者

C# Combining conditions

if (thedata[m].ToUpper() == thedata[m] && thedata[m].Contains(" ") || (thedata[m - 1] == "" && thedata[m].Contains(S开发者_如何学运维peaking)))

Instead of rewriting everything up to Contains(), is there a way to combine it into one Contains()?

Would this be done using an string[] where each string of characters I want to put into the Contains() is its own string?

Can these be combined, if so, how?


Not out of the box. String.Contains has no overload for that purpose.

What you could do is write an extension method yourself so that you could use it several times.

public static class StringExtensions
{
      public static bool Contains(this string input, params string[] queries)
      {
           foreach (string s in queries)
           {
                if (!input.Contains(s)) return false;
           }

           return true;
      }
}

After which you could simply call

if (thedata[m].Contains(" ", "Speaking")) && {rest of your expression})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜