简体   繁体   English

如何检查string.contains两种方法

[英]how to check string.contains Either Ways

How do i see if string1 contains string2 OR string2 contains string1 OR String 3 contains string 1 OR String 1 contains string 3 OR String 2 contains string 3 OR string 3 contains String 2 in single line? 如何查看string1包含string2或string2包含string1或String 3包含字符串1或String 1包含字符串3或String 2包含字符串3或String 3包含字符串2在单行中?

like i want to do 就像我想做的

if ( string1.contains(string2) || string2.contains(string1) || string3.contains(string1) string3.contains(string2) || .. and so on.. ) 
{

}

in single check without or clause. 在没有or子句的单检查中。 In reality, i have to do this check multiple times and multiple places. 实际上,我必须多次多次进行此检查。 So just wondering if there is a better way. 因此,只是想知道是否有更好的方法。

I have updated my business logic or the reason for this logic. 我已经更新了我的业务逻辑或这种逻辑的原因。 We have 6 Sites and we will get just the page names of the sites. 我们有6个站点,我们将仅获得站点的页面名称。 i have to check the site names are similar. 我必须检查站点名称是否相似。 The only problem is.. i donot get the site names in any particular pattern. 唯一的问题是..我没有以任何特定模式获得站点名称。 like for eg: 例如:

String1 = "/search-results"
string2= "www.foo.com/search-results?Id=1234"
string3 = "search-results"
string4= "/search-results?Id=1234"

and if you look at my values, you will note that if you compare any two strings with OR clause.. they will be true. 如果查看我的值,您会注意到,如果将任何两个字符串与OR子句进行比较,则它们为true。

Put your strings into an array or a list, then: 将您的字符串放入数组或列表中,然后:

bool result = yourStrings
            .Where((x, idx) => 
                yourStrings.Where((y, index) => idx != index && x.Contains(y))
                .Any())
                .Any();

Explanation: 说明:

This query will take each string, and compare them with others and returns a result that indicates whether any of the strings is contains another string in the collection. 该查询将获取每个字符串,并将它们与其他字符串进行比较,并返回一个结果,该结果指示该字符串中的任何字符串是否包含集合中的另一个字符串。

For example consider we have three strings foo , bar and fooBar , the steps would be like the following: 例如, fooBar我们有三个字符串foobarfooBar ,步骤将如下所示:

  1. Take "foo" and check if it contains bar or fooBar (false) "foo"检查其是否包含barfooBar (假)
  2. Take "bar" and check if it contains foo or fooBar (false) "bar"并检查其是否包含foofooBar (假)
  3. Take "fooBar" and check if it contains foo or bar (true because of foo ) "fooBar"并检查它是否包含foobar (由于foo而为true)

I wish there is an overload of Any that accepts the index parameter... 我希望有一个接受index参数的Any重载...

Edit: here is also more efficient implementation for larger collections: 编辑:对于较大的集合,这也是更有效的实现:

public static bool ContainsAny(IList<string> source)
{
     int count = source.Count;
     for (int i = 0; i < count; i++)
     {
          for (int j = 0; j < count; j++)
          {
              if (i != j && source[i].Contains(source[j]))
              {
                  return true;
              }
          }
      }

      return false;
}

You can have an helper method and use it : 您可以使用一个辅助方法并使用它:

public static void SomeContainsAnOther(this IEnumerable<string> @this)
{
   @this.Any(v=>@this.Count(other=>other.Contains(v)) > 1);
}

And use it : 并使用它:

new string[] {string1,string2,string3,..stringN}.SomeContainsAnOther();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM