简体   繁体   English

C# - 检查字符串是否包含相同顺序的另一个字符串的字符

[英]C# - Check if string contains characters of another string at the same order

I would like to check if a string contains the characters of another string (returning true or false), but it needs to be in the "right" order but not necessarily contiguous. 我想检查一个字符串是否包含另一个字符串的字符(返回true或false),但它需要处于“正确”的顺序,但不一定是连续的。

Example: 例:

String firstWord = "arm";
String secondWord = "arandomword"; //TRUE - ARandoMword

String thirdWord = "road"; //FALSE - ARanDOmword

The word "arandomword" contains the letters of the word "road" but it's not possible to write it, because they are not at the right order. 单词“arandomword”包含单词“road”的字母,但不可能写出来,因为它们的顺序不正确。

Anyone, please? 有人吗?

Use regex. 使用正则表达式。 Something simple that passes your tests in linqpad: 在linqpad中通过测试的简单方法:

void Main()
{
    String firstWord = "arm";
    String secondWord = "arandomword"; //TRUE - ARandoMword

    String thirdWord = "road";

    Regex.IsMatch(secondWord,makeRegex(firstWord.ToCharArray())).Dump();
}

// Define other methods and classes here
String makeRegex(char[] chars)
{
    StringBuilder sb = new StringBuilder();
    foreach (var element in chars.Select(c => Regex.Escape(c.ToString()))
        .Select(c => c + ".*"))
    {
        sb.Append(element);
    }
    return sb.ToString();
}

you could define an extension method like this: 你可以定义一个像这样的扩展方法:

public static class StringExtensions
{
    public static bool ContainsWord(this string word, string otherword)
    {
        int currentIndex = 0;

        foreach(var character in otherword)
        {
            if ((currentIndex = word.IndexOf(character, currentIndex)) == -1)
                return false;
        }

        return true;
    }
}

and call it as expressive as: 并称之为表达:

String firstWord = "arm";
String secondWord = "arandomword"; //TRUE - ARandoMword
String thirdWord = "road"; //FALSE - ARanDOmword

var ret = secondWord.ContainsWord(firstWord); // true
var ret2 = thirdWord.ContainsWord(firstWord); // false

Something like this? 像这样的东西?

bool HasLettersInOrder(string firstWord, string secondWord)
{
    int lastPos = -1;
    foreach (char c in firstWord)
    {
        lastPos++;
        while (lastPos < secondWord.Length && secondWord[lastPos] != c)
            lastPos++;
        if (lastPos == secondWord.Length)
            return false;
    }
    return true;
}

I can not check right now, but something along the lines: 我现在无法检查,但有些事情是这样的:

int i = 0, j = 0;

while(i < first.Length && j < second.Length)
{
   while(first[i] != second[j] && j < second.Length) j++;
   i++;
   j++
}

bool b = i == first.Length;

Thanks for all the replies. 感谢所有的答复。 I've tried and tried and I did it my way. 我已经尝试过,我按照自己的方式做到了。 Definitively it's not the shortest way to do it, but at least it's working: 确切地说,这不是最短的方式,但至少它的工作:

    public static Boolean checkWords(String smallerWord, String biggerWord)
    {
        int position = 0;
        bool gotFirst = false;
        bool gotAnother = false;
        int positionBigger = 0;
        String word = "";

        for(int positionSmaller = 0; positionSmaller < smallerWord.Length; positionSmaller++)
        {
            if(!gotFirst)
            {
                if(biggerWord.Contains(smallerWord[positionSmaller].ToString()))
                {
                    position = biggerWord.IndexOf(smallerWord[positionSmaller].ToString());
                    gotFirst = true;
                    word = smallerWord[positionSmaller].ToString();
                }
            }
            else
            {
                gotAnother = false;
                positionBigger = position + 1;

                while(!gotAnother)
                {
                    if(positionBigger < biggerWord.Length)
                    {
                        if(biggerWord[positionBigger].ToString().Equals(smallerWord[positionSmaller].ToString()))
                        {
                            position = positionBigger;
                            gotAnother = true;
                            word += smallerWord[positionSmaller].ToString();
                        }
                        else
                        {
                            positionBigger++;
                        }
                    }
                    else
                    {
                        gotAnother = true;
                    }
                }
            }
        }

        if(smallerWord.Equals(word))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

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

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