简体   繁体   English

在字符串中搜索单词并进行比较

[英]Searching for words in a string and comparing them

I can't come up with a word search code that would still output the word when the position of the word changes我无法想出一个单词搜索代码,当单词的 position 发生变化时,该单词仍然是 output


    class Program
    {

        static void Main(string[] arg)
        {

            Console.OutputEncoding = System.Text.Encoding.Unicode;
            Console.InputEncoding = System.Text.Encoding.Unicode;

            var srd = new MultiSet<Garders>()
            {
                new Garders("ірис, троянда, айстра, півонія, жоржин"),
                new Garders("ірис, троянда, айстра, півонія, жоржин, хризантема, гладіолус"),
                new Garders("ірис, троянда, айстра, півонія, гладіолус")

            };
            MultiSet<Garders>.Enumerator e = srd.GetEnumerator();
            string[] temp = new string[3];
            for (int i = 0; i < temp.Length; i++)
            {
                e.MoveNext();
                temp[i] = e.Current.flower;
            }
            e.Reset();
            while (e.MoveNext())
            {
                
                string[] srt = e.Current.flower.Split();

                foreach (var item in srt)
                {
                    if (temp[0].Contains(item) == temp[1].Contains(item) 
                        && temp[1].Contains(item) == temp[2].Contains(item))
                            Console.WriteLine(item);
                }
                Console.WriteLine();

            }
                
        }

    }
}

My code only outputs the same words if they are in the same positions如果它们位于相同的位置,我的代码只会输出相同的词

You have written very complex logic, it should be very easy if you read the problem statement carefully.你已经写了非常复杂的逻辑,如果你仔细阅读问题陈述,应该很容易。

Based on the discussion in a comment, you want to compare two collections whether they have the same items in the same order.根据评论中的讨论,您想比较两个 collections 是否具有相同顺序的相同项目。

You could use List .您可以使用List

and compare like this.并这样比较。

List<string> ls1 = new List<string>() { "1", "2"};
List<string> ls2 = new List<string>() { "1", "2" };
bool isEqual = ls1.SequenceEqual(ls2);

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

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