Tomas Wilson
Tomas Wilson

Reputation: 157

Get unused chars in string?

I am really new to Programming and I just started and learned some basics for C#. I just tried to write a method that checks if some special chars are NOT included in a string. My result is this code:

static string GetUnused(string s)
{
    /*
     *  Propably will get confused if s contains '#' char... how2fix?
     */
    char[] signs = { '!', '§', '$', '%', '&', '/', '(', ')', '=', '?' };
    foreach (char c in s)
    {
        if (c == '!') signs[0] = '#';
        if (c == '§') signs[1] = '#';
        if (c == '$') signs[2] = '#';
        if (c == '%') signs[3] = '#';
        if (c == '&') signs[4] = '#';
        if (c == '/') signs[5] = '#';
        if (c == '(') signs[6] = '#';
        if (c == ')') signs[7] = '#';
        if (c == '=') signs[8] = '#';
        if (c == '?') signs[9] = '#';
    }
    string ret = string.Empty;
    foreach (char x in signs)
    {
        if (x == '#') ret += "";
        else ret += x;
    }
    return ret;

but I am pretty sure that is not a good solution to my Problem... How do I mange to solve this in a more elegant way? Thank you for your Answers.

Upvotes: 5

Views: 302

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39946

You could use Except:

private static string GetUnused(string s)
{
    char[] signs = {'!', '§', '$', '%', '&', '/', '(', ')', '=', '?'};
    var ret = signs.Except(s);
    return String.Join("",ret);
}

Upvotes: 4

Alexander Petrov
Alexander Petrov

Reputation: 14231

Yet another answer

static string GetUnused(string s)
{
    char[] signs = { '!', '§', '$', '%', '&', '/', '(', ')', '=', '?' };
    var set = new HashSet<char>(signs);

    foreach (char c in s)
        set.Remove(c);

    return string.Concat(set);
}

HashSet is very fast.

If the input parameter can be very large then next version will be even more profitable, in some cases

static string GetUnused(string s)
{
    char[] signs = { '!', '§', '$', '%', '&', '/', '(', ')', '=', '?' };
    var set = new HashSet<char>(signs);

    foreach (char c in s)
    {
        set.Remove(c);
        if (set.Count == 0)
            return string.Empty;
    }
    return string.Concat(set);
}

Upvotes: 1

Tim Ashton
Tim Ashton

Reputation: 446

If you stored signs as a list<char> you could use RemoveAll to remove any item that exists in the parameter of the method like this:

static string getunused(string param)
{
    list<char> signs = new list<char>(){ '!', '§', '$', '%', '&', '/', '(', ')', '=', '?' };
    signs.removeall(c => param.contains((c.tostring())));
    return new string(signs.toarray());
}

Upvotes: 0

Related Questions