3

Let's say I have such a string: [a b c d e f]. I would like to extract those characters and create a char array. Here is how I did it. Seems to be working:

var test = GetThemOut("[a b c d e f]");

// ...

public static char[] GetThemOut(string array)
{
    return array
        .Trim('[', ']')
        .Split(' ')
        .Select(x => x[0])
        .ToArray();
}

Is there a simpler and a cleaner way to accomplish this? Well, it seems for me that my solution is really stupid and the language could provide a better one.

Still learning the language, trying to grasp tricks. Thanks.

EDIT

Alternative, as suggested:

public static char[] GetThemOut(string array)
{
    return array
        .Trim('[', ']')
        .Replace(" ", "")
        .ToCharArray();
}
6
  • 3
    MSDN String.ToCharArray is what you're looking for. Use the MSDN! Also there's context sensitive help in VS that should have clued you in on it.
    – t0mm13b
    Commented Nov 10, 2015 at 23:12
  • Are there any other characters your likely to want to remove?
    – Darren S
    Commented Nov 10, 2015 at 23:13
  • @DarrenS I just want to extract all the characters that are between those brackets [...]
    – ebvtrnog
    Commented Nov 10, 2015 at 23:14
  • @Randolph No .ToCharArray() will work. Instead of your Split you can Replace to remove your spaces then use .ToCharArray()
    – Darren S
    Commented Nov 10, 2015 at 23:18
  • 1
    You could also use LINQ: array.Except(new[]{'[',']',' ']}.ToArray()
    – Ian Mercer
    Commented Nov 10, 2015 at 23:27

4 Answers 4

2

Here's another flavor which uses Char.IsWhiteSpace:

var test = GetThemOut("[a b c d e f]");

public static char[] GetThemOut(string array)
{
    return array.Trim('[', ']')
                .Where(c => !Char.IsWhiteSpace(c))
                .ToArray();                          
}
1

Like this:

    public static char[] GetThemOut(string array)
    {
        return array.Trim('[', ']').Replace(" ", string.Empty).ToCharArray();
    }
1

You can simply filetr it with Char.IsLetter or Char.IsLetterOrDigit:

public static char[] GetThemOut(string array)
{
    return array.Where(c => Char.IsLetter(c)).ToArray();
}
0

To understand parsing from a traditional perspective; consider this example:

    static char[] ParseCharSequence(string s)
    {
        s = s.Trim();

        if (s.Length > 1 && s[0] == '[' && s[s.Length - 1] == ']')
        {
            char[] array = new char[s.Length / 2];

            int depositor = 0;
            int offset = -1;
            int boundary = s.Length - 1;

            for (int i = 1; i < boundary; i++)
            {
                if (s[i] != ' ')
                {
                    if (i == offset + 1)
                    {
                        throw new ArgumentException(String.Format("Conflict at index {0}.", i));
                    }

                    array[depositor++] = s[i];
                    offset = i;
                }
            }

            Array.Resize(ref array, depositor);

            return array;
        }

        return null;
    }

This function is 10 times faster than:

return s.TrimStart('[').TrimEnd(']').Where(x => !Char.IsWhiteSpace(x)).ToArray();

Either way, the suggested format is nonsensical because characters are implicitly separable. However, if you wish to separate strings as a next assignment, then you would indeed require a delimiter. Note that space is not recommendable as a delimiter, because it has no explicit significance, reading it causes eye-sore.

Not the answer you're looking for? Browse other questions tagged or ask your own question.