user3581978
user3581978

Reputation:

Comparing char arrays method

I've been playing space engineers which has been epic since they added in-game programming, I'm trying to make a gps auto-pilot navigation script and have to get the block positions finding the blocks by name looking for a smaller string within their bigger string name. I wrote this method to find a small string (word) in a larger string (name of the block):

bool contains(string text, string wordInText)
{
    char[] chText = text.ToCharArray();
    char[] chWord = wordInText.ToCharArray();

    int index = 0;
    for(int i = 0 ; i < chText.Length - chWord.Length ; i++)    
        for(int j = 0; j < chWord.Length;j++,index++)
            if (chWord[0] == chText[i])
                index = i;
            else if (chWord[j] == chText[index]){}                
            else if (index == chWord.Length-1)
                return true;
            else break;

    return false; 
}

Am I even doing it right, should I be doing it another shorter way?

Upvotes: 1

Views: 834

Answers (3)

Corey
Corey

Reputation: 16564

As already mentioned, the String class already has a Contains method that should give you what you need.

That said, your code doesn't work. I can see where you're going with it, but it's just not going to work. Stepping through it in a proper dev environment would show you why, but since this is more in the way of a script that's probably not an option.

So... the basic idea is to iterate through the string you're searching in, looking for matches against the string your searching. Your outer for statement looks fine for this, but your inner statements are a bit messed up.

Firstly, you're doing the first character check repeatedly. It's wasteful, and misplaced. Do it once per iteration of the outer loop.

Second, your exit condition is going to fire when the first character of wordInText matches the characters at index wordInText.Length in text which is not apparently what you want.

In fact you're all tripped up over the index variable. It isn't actually useful, so I'd drop it completely.

Here's a similar piece of code that should work. It is still much slower than the library String.Compare method, but hopefully it shows you how you might achieve the same thing.

for (int i = 0; i <= chText.Length - chWord.Length; i++)
{
    if (chText[i] == chWord[0])
    {
        int j;
        for (j = 0; j < chWord.Length; j++)
        {
            if (chText[i + j] != chWord[j])
                break;
        }
        if (j == chWord.Length)
            return true;
    }
}
return false;

Upvotes: 0

Brady Holt
Brady Holt

Reputation: 2924

This is simple with .Contains() which returns a bool.

text.Contains(wordInText);

Upvotes: 1

DarkShadow44
DarkShadow44

Reputation: 1

If you simply want to check if a string contains an other string, then you can use string.Contains, the string class already provides a bunch of methods for string operations.

Upvotes: 0

Related Questions