【问题标题】:Checking if a string contains words in a specific order检查字符串是否包含特定顺序的单词
【发布时间】:2014-12-07 15:32:55
【问题描述】:

这是一个有趣的问题,假设您有一个包含 3 个句子的列表,例如:

比尔猫有

比尔有一只猫

猫有比尔

我将如何使用 .Contains() 或任何其他方法来检查列表中的句子是否包含特定顺序的单词,算法如下所示:

1) 通过foreach循环运行句子列表

2) 检查句子中是否包含此顺序的单词 => Bill + had + cat

3) 返回那个句子

所以每个其他句子都返回 false,因为单词的顺序不同。伙计们,有什么想法可以实现吗? :)

【问题讨论】:

  • 你可以用正则表达式从列表中找到匹配的字符串
  • “词序”条件能满足吗? @jadavparesh06
  • 是的,条件可以通过正则表达式指定
  • 在上面的循环中,我们需要返回真,因为只有比尔有帽子???对于其他语句,我们需要返回 false。?这是你的问题吗?
  • 是的,先生! @sarathkumar

标签: c# string foreach conditional-statements


【解决方案1】:

尝试下面的解决方案它可以工作。

class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            list.Add("Bill cat had");
            list.Add("Bill had a cat");
            list.Add("Bill had cat");
            list.Add("Cat had Bill");
            Regex rex = new Regex(@"((Bill)).*((had)).*((cat))");

            foreach (string str in list)
            {
                if (rex.IsMatch(str))
                {
                    Console.WriteLine(str);
                }
            }
            Console.ReadLine();
        }
    }

【讨论】:

  • 它在我的控制台应用程序上正常工作,不知道为什么它会为你抛出异常。
【解决方案2】:
List<string> ss = new List<string>();
ss.Add("Bill had cat");
string inputstring = "In 2012, Bill had a cat";
foreach (string s in ss)
{
    string[] split = s.Split(' ');
    string regex = ".*";
    for(int a = 0; a<split.Length;a++)
        regex += split[a]+".*";
    Match temp = Regex.Match(inputstring, regex);
    if (temp.Success)
    {
        MessageBox.Show("String \"" + inputstring  + "\" matches");
    }
}

试试这是否可行,这是区分大小写的,所以要考虑到这一点

【讨论】:

  • 嗯,它实际上为“Bill cat has”返回 true。如何更改它,我有点不明白该代码背后的逻辑先生
  • 你输入的字符串是什么?
  • 因为我需要它为问题中提到的条件返回 true,所以我的输入字符串是“2012 年,比尔有一只猫”。但它返回 false。
  • 但是“Bill cat had”对于那个字符串是假的还是?我的意思是猫来了之后。字符串“Bill had a cat”是正确的,因为它是按这个顺序排列的。
  • 是的,这将是错误的,我希望它是正确的,当且仅当它一个接一个地包含“Bill + had + Cat”这个词。类似于:比尔 > 拥有 > 猫。抱歉一再唠叨:)
【解决方案3】:

这对我有用。

class Program
{
    static List<string> sentences = new List<string>();
    static List<string> pattern = new List<string>();
    static List<string> results = new List<string>();

    static void Main(string[] args)
    {
        //sentences are in order
        sentences.Add("Bill cat had");
        sentences.Add("Bill had a cat");
        sentences.Add("Cat had Bill");
        sentences.Add("Bill had cats");

        //patters are in order
        pattern.Add("Bill");
        pattern.Add("had");
        pattern.Add("cat");

        // call the searchString method
        results = searchString(sentences, pattern);

        foreach (string res in results)
        {
            Console.WriteLine(res);
        }

        Console.Read(); // just keep program running when debugged
    }

    static List<string> searchString(List<string> sentences, List<string> patterns)
    {
        bool result = false;
        List<string> resultLIst = new List<string>();

        foreach (string sen in sentences)
        {
            int j = 0;
            foreach (string pat in pattern)
            {
                if (sen.Contains(pat))
                {
                    if (j <= sen.IndexOf(pat))
                    {
                        result = true;
                    }
                    else
                    {
                        result = false;
                        break;
                    }
                    j = sen.IndexOf(pat);
                }
                else
                {
                    result = false;
                    break;
                }
            }

            if (result)
                resultLIst.Add(sen); // get the matching sentence
        }

        return resultLIst; // return matchin sentences
    }

}

【讨论】:

    【解决方案4】:

    也检查这个答案

     protected void Page_Load(object sender, EventArgs e)
            {
                string message = "";
                string[] list = new string[] { "Bill cat had", "Bill had a cat", "Cat had Bill" };
                foreach (var item in list)
                {
                    string[] splitString = item.Trim().Split(' ');
                    int i = 0; bool IsValid = true;
                    int count = 0;
                    foreach (var sindividual in splitString)
                    {
                        int j = CheckMatched(sindividual);
                        if (j != 0)
                        {
                            if (j > i)
                            {
                                i = j;
                                count++;
                            }
                            else
                            {
                                IsValid = false;
                            }
                        }
                    }
    
    
                    if (count >= 3 && IsValid)
                    {
                        message += item + "   " + "yes it has t proper order \n";
                    }
                    else
                    {
                        message += item + "   " + "Doesnt have proper order \n";
                    }
                }
    
                lblMessage.Text = message;
    
    
            }
    
    
            int CheckMatched(string sStringtoCheck)
            {
                sStringtoCheck = sStringtoCheck.Trim().ToLower();
    
                if (sStringtoCheck.Contains("bill"))
                {
                    return 1;
                }
                else if (sStringtoCheck.Contains("had"))
                {
                    return 2;
                }
                else if (sStringtoCheck.Contains("cat"))
                {
                    return 3;
                }
                else return 0;
            }
    

    这也很好用.. 但有点大

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 2011-05-20
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多