Check if a word is before another Let's consider a method like this:

public bool IsWordABeforeWordB(string Text, string WordA, string WordB)
{
   ...
}


By Word I mean a string composed of more than one character.

Test cases:

[TestMethod]
public void IsWordABeforeWordB_ShouldBeTrue()
{
    string Text = "CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([IDTest] ASC)";
    bool result = AtomicCheckMethods.IsWordABeforeWordB(Text, "CLUSTERED", "ASC");
    result.Should().BeTrue();
}

[TestMethod]
public void IsWordABeforeWordB_ShouldBeFalse()
{
    string Text = "CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([IDTest] ASC)";
    bool result = AtomicCheckMethods.IsWordABeforeWordB(Text, "PRIMARY KEY", "CONSTRAINT");
    result.Should().BeFalse();
}

[TestMethod]
public void IsWordABeforeWordB_IgnoreCase_ShouldBeTrue()
{
    string Text = "CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([IDTest] ASC)";
    bool result = AtomicCheckMethods.IsWordABeforeWordB(Text, "clustered", "asc");
    result.Should().BeTrue();
}

[TestMethod]
public void IsWordABeforeWordB_IgnoreCase_ShouldBeFalse()
{
    string Text = "CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([IDTest] ASC)";
    bool result = AtomicCheckMethods.IsWordABeforeWordB(Text, "primary key", "constraint");
    result.Should().BeFalse();
}


What I tried:

public static bool IsWordABeforeWordB(string Text, string WordA, string WordB)
{
    string stringBeforeWordB = Text.Substring(0, Text.IndexOf(WordB, StringComparison.InvariantCultureIgnoreCase));
    return stringBeforeWordB.IndexOf(WordA, StringComparison.InvariantCultureIgnoreCase) &gt;= 0;
}


Can I do it better?
You're naming your parameters incorrectly. The naming guidelines for parameters specify camel casing.

Your method declaration should look like this:

public static bool IsWordABeforeWordB(string text, string wordA, string wordB)




Your current implementation also has a bug. You are assuming that the source text contains wordB - if it doesn't you'll get an ArgumentOutOfRangeException on the substring.



I'd suggest that performance probably doesn't matter here so you may as well just do it the long way:

public static bool IsWordABeforeWordB(string text, string wordA, string wordB)
{
    // Omitted checking text, wordA and wordB for null.
    var indexOfFirstWordA = text.IndexOf(wordA, StringComparison.OrdinalIgnoreCase);
    if (indexOfFirstWordA == -1)
    {
         // throw exception or return false.
    }
    var indexOfLastWordB = text.LastIndexOf(wordB, StringComparison.OrdinalIgnoreCase);
    if (indexOfLastWordB == -1)
    {
        // throw exception or return false.
    }

    // edited based on holroy's comment.
    // Otherwise we'd be wrong if wordB was a substring of wordA
    // e.g. wordA: hello, wordB: lo 
    return indexOfFirstWordA + wordA.Length &lt; indexOfLastWordB;

}


I've assumed that IsWordABeforeWordB("Sample text is sample", "text", "sample") should return true as "text" does appear before one of the "sample"s. If that's not the behaviour you were going for you can change the LastIndexOf to a simple IndexOf



The definition of a "word" is a bit loose here. You could do a Regular Expression match to get the indexes which would allow you to use the "\b" word anchor. I've just assumed (as your OP) that it's simply a sequence of characters.