繁体   English   中英

C#-使用另一个字符串数组中的字符串搜索字符串数组

[英]c# - search a string array using strings from another string array

就像标题中一样。 我得到了一个字符串数组和第二个字符串数组。 我想以这种模式显示结果:第一个数组的第一个元素-然后出现在第一个数组的第一个元素中的第二个数组的所有元素。 在第一数组的第二个元素和第二数组的所有元素之后出现在第一数组的第二个元素之后。 等等。 例如:

string[] arrayA = {"Lorem ipsum dolor sit amet, justo", "notgin like good cold beer"};
string[] arrayB = {"justo","beer","lorem"}
for (int i = 0; i < arrayA.Length; i++)
   {
      Console.WriteLine(arrayA[i]);

       for (int j = 0; j < arrayB.Length; j++)
       {
          int controlIndex = arrayA[i].IndexOf(arrayB[j]);
          if (controlIndex != -1)
          {
               Console.Write(" :--contains-->" + arrayB[j]);
          }

    }

}

因此结果应如下所示:

  • Lorem ipsum dolor坐在amet,justo:-包含-> justo,lorem
  • notgin就像优质的冷啤酒:-包含-> 啤酒

但是我的结果是:-Lorem ipsum dolor坐着,justo:-包含-> justo -notgin像优质冷啤酒:-包含-> beer

因此,如您所见,没有列出lorem

如果您将问题分解成若干部分,这一点都不难。 首先,摆脱对数组和索引的处理。 只需使用IEnumerable<T> ,它将使您的生活更轻松。

这是我的看法:

首先,您要从数组needles haystack找到所有字符串。

public static IEnumerable<string> MatchingStrings(string haystack, IEnumerable<string> needles)
{
    return needles.Where(needle => haystack.Contains(needle));
}

这将返回一个IEnumerable所有字符串从needles是一部分haystack

然后,您只想简单地遍历所有搜索字符串,我称其为haystacks

    static void Main(string[] args)
    {
        var haystacks = new[] {
            "Lorem ipsum dolor sit amet, justo",
            "notgin like good cold beer"
        };

        var needles = new[] {"justo", "beer", "lorem"};

        foreach (var haystack in haystacks) {
            Console.Write(haystack + "  contains --> ");
            var matches = MatchingStrings(haystack, needles);

            Console.WriteLine(String.Join(",", matches));
        }

        Console.ReadLine();
    }

请注意, String.Contains() 区分大小写 因此,“ Lorem”将与“ lorem”不匹配。 如果您想要这种行为,则必须先将它们转换为小写。

public static IEnumerable<string> MatchingStringsCaseInsensitive(string haystack, IEnumerable<string> needles)
{
    var h = haystack.ToLower();
    return needles.Where(needle => h.Contains(needle.ToLower()));
}
string[] arrayA = {"Lorem ipsum dolor sit amet, justo", "notgin like good cold beer"};
string[] arrayB = {"justo","beer","lorem"};

foreach (var s in arrayA)
{
    Console.Write(s + " contains: " +
                  string.Join(",", arrayB.Where(s.Contains)));
}

如果您想忽略大小写:

foreach (var s in arrayA)
{
    Console.Write(s + " contains: " +
                  string.Join(",", arrayB.Where(x =>
                      s.IndexOf(x, StringComparison.OrdinalIgnoreCase) != -1)));
}
foreach(var a in arrayA)
{
    Console.WriteLine("a: " + a);
    Console.WriteLine("bs: " + 
        String.Join(", ", arrayB.Where(b => a.IndexOf(b) > -1)));
}

另外,如果您不关心大小写,则a.IndexOf(b)将是a.IndexOf(b, StringComparison.OrdinalIgnoreCase)

这是Linq解决方案:

var result = arrayA.Select(a => new{
    A = a,
    bContains = arrayB.Where(b => a.IndexOf(b, 0, StringComparison.CurrentCultureIgnoreCase) > -1)            
});

foreach(var x in result)
{
    Console.WriteLine("{0}:--contains-->{1}", x.A, string.Join(",", x.bContains));
}

这是一个演示: http : //ideone.com/wxl6I

这是我的尝试

string[] arrayA = {"lorem ipsum dolor sit amet, justo", "notgin like good cold beer"};
string[] arrayB = {"justo", "beer", "lorem"};

foreach (var item in from a in arrayA from b in arrayB where a.Contains(b) select new {a, b})
{
    Console.WriteLine(item.a);
    Console.WriteLine(item.b);
}

注意: Contains是区分大小写的比较,您需要编写一个自定义比较器(因为其他答案已经完成)

Lorem是大写的。 尝试使用不区分大小写的搜索: .indexOf(string, StringComparison.CurrentCultureIgnoreCase)

我已经给了您所有可能的答案,但是contains方法会产生一个问题,在下述情况下,它也会返回true。

reference_string = "Hello Stack Overflow"
test_string = "Over"

所以尽量避免包含,因为包含方法将

“返回一个值,该值指示指定的System.String对象是否出现在此字符串中”

注意:添加了StringComparer.OrdinalIgnoreCase以便区分大小写。

/// <summary>
        /// Compares using binary search
        /// </summary>
        /// <param name="input"> search input</param>
        /// <param name="reference"> reference string</param>
        /// <returns> returns true or false</returns>
        public bool FatMan(string input, string reference)
        {
            string[] temp = reference.Split();
            Array.Sort(temp);
            List<string> refe = new List<string> { };
            refe.AddRange(temp);

            string[] subip = input.Split();
            foreach (string str in subip)
            {
                if (refe.BinarySearch(str, StringComparer.OrdinalIgnoreCase) < 0)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// compares using contains method
        /// </summary>
        /// <param name="input"> search input</param>
        /// <param name="reference"> reference string</param>
        /// <returns> returns true or false</returns>
        public bool Hiroshima(string input, string reference)
        {
            string[] temp = reference.Split();
            Array.Sort(temp);
            List<string> refe = new List<string> { };
            refe.AddRange(temp);
            string[] subip = input.Split();

            foreach (string str in subip)
            {
                if (!refe.Contains(str, StringComparer.OrdinalIgnoreCase))
                {
                    return false;
                }
            }

            return true;
        }


        public bool Nakashaki(string input, string reference)
        {
            string[] temp = reference.Split();
            Array.Sort(temp);
            List<string> refe = new List<string> { };
            refe.AddRange(temp);
            string[] subip = input.Split();

            int result = (from st in subip where temp.Contains(st, StringComparer.OrdinalIgnoreCase) select st).Count();

            if (result <= 0)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// compares using contains method
        /// </summary>
        /// <param name="input"> search input</param>
        /// <param name="reference"> reference string</param>
        /// <returns> returns true or false</returns>
        public bool LittleBoy(string input, string reference)
        {
            string[] subip = input.Split();
            foreach (string str in subip)
            {
                if (!reference.Contains(str))
                {
                    return false;
                }
            }

            return true;
        }
 bool oupt ;
 string[] strarray1 = new string[3]{"abc","def","ghi" };
 string[] strarray2 = new string[4] { "648", "888", "999", "654" };
 if (strarray1.All(strarray.Contains))
    oupt = true;
 else
    oupt = false;

暂无
暂无

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

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