简体   繁体   中英

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.

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.

Anyone, please?

Use regex. Something simple that passes your tests in 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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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