BCS
BCS

Reputation: 78683

How to find the index of the first char in a string that is not in a list

I know I can loop over the string or build a regex or invert the set (ASCII isn't that big after all) and search for the first instance of that, but Yuck.

What I'm looking for is a nice one liner.

fewer features is better, LINQ is out (for me, don't ask, it's a long story)


The solution I'm going with (unless I see something better)

static int FirstNotMeta(int i, string str)
{
    for(; i < str.Length; i++)
        switch(str[i])
        {
            case '\\':
            case '/':
            case '.':
                continue;
            default:
                return i;
        }
    return -1;
}

OK, I cheated, I know in advance what char's I care about.

Upvotes: 1

Views: 6560

Answers (4)

Adam Liss
Adam Liss

Reputation: 48330

Will this C/C++ example work for you:

char *strToSearch = "This is the one liner you want"
char *skipChars = "Tthise";
size_f numToSkip = strcspn(strToSearch, skipChars);

The strcspn() function scans a string for the complement of the specified set. It returns the number of initial characters that do not include a character in the set.

Upvotes: 1

Jeff B
Jeff B

Reputation: 1866

This works:

public static char FindFirstNotAny(this string value, params char[] charset)
{
    return value.TrimStart(charset)[0];
}

Upvotes: 7

Robert Wagner
Robert Wagner

Reputation: 17793

If you don't have access to LINQ, I think you may just have to write a static method with a loop (which is probably more efficient than LINQ anyway. Remember the compiler will inline small methods when possible.

The simplest non-LINQ I can come up with is below. I recommend adding braces so scope and the blocks are clear:

public static char? GetFirstChar(string str, char[] list)
{
    foreach (char c in str) if (!list.Contains(c)) return c;
    return null;
}

With C# 3.0 and LINQ:

char[] list = { 'A', 'B' };
string str = "AABAGAF";

char first = str.ToArray().Where(c => !list.Contains(c)).FirstOrDefault();

In that case, if there is no non-list character, first will equal 0x0000 (or the character null). You could do this:

char? first = str.ToArray().Cast<char?>().Where(
    c => !list.Contains(c.Value)).FirstOrDefault();

Then first will be null if there are no matches. This can also be written as:

var query = from char c in str
            where !list.Contains(c)
            select (char?)c;
char? first = query.FirstOrDefault();

Upvotes: 2

Joe Strommen
Joe Strommen

Reputation:

Not all that efficient, but:

char f(string str, IEnumerable<char> list)
{
  return str.ToCharArray().First(c => !list.Contains(c))
}

Upvotes: 1

Related Questions