检查字符串是否包含按特定顺序排列的单词

本文关键字:排列 单词 定顺序 字符串 是否 包含按 检查 | 更新日期: 2023-09-27 18:26:16

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

Bill cat有

比尔有一只猫

Cat有票据

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

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

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

3)返回该句子

因此,由于单词的顺序不同,每隔一句都会返回false。关于实现这一点,各位有什么想法吗?:)

检查字符串是否包含按特定顺序排列的单词

尝试下面的解决方案它有效。

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();
        }
    }
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");
    }
}

如果这有效,请尝试,这是区分大小写的,所以请考虑

这对我有效。

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
    }
}

也检查这个答案

 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;
        }

这也很好。。但有点大