簡體   English   中英

C# - 檢查字符串是否包含相同順序的另一個字符串的字符

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

我想檢查一個字符串是否包含另一個字符串的字符(返回true或false),但它需要處於“正確”的順序,但不一定是連續的。

例:

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

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

單詞“arandomword”包含單詞“road”的字母,但不可能寫出來,因為它們的順序不正確。

有人嗎?

使用正則表達式。 在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();
}

你可以定義一個像這樣的擴展方法:

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

並稱之為表達:

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

像這樣的東西?

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

我現在無法檢查,但有些事情是這樣的:

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;

感謝所有的答復。 我已經嘗試過,我按照自己的方式做到了。 確切地說,這不是最短的方式,但至少它的工作:

    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