TheAlbear
TheAlbear

Reputation: 5595

Find if a string starts between two sections of Alphabet

What I am trying to do if find out if my string start is between two letters in the alphabet

What I want is something like

mystring.StartsWith("a") but not greater that mystring.StartsWith("au")

Upvotes: 0

Views: 350

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502816

That's fairly simple:

StringComparer comparer = StringComparer.Ordinal;
if (comparer.Compare(myString, "a") >= 0 &&
    comparer.Compare(myString, "au") < 0)
{
    // Do stuff
}

That will include "atzzzz" but not "au" itself. Adjust bounds as required - and likewise choose a different StringComparer if necessary (e.g. a case-insensitive or culture-sensitive one).

Upvotes: 3

Related Questions