1

I have a string which I would like to split on a particular delimiter and then remove starting and trailing whitespace from each member. Currently the code looks like:

string s = "A, B, C ,D";
string[] parts = s.Split(',');
for(int i = 0; i++; i< parts.Length)
{
     parts[i] = parts[i].Trim();
}

I feel like there should be a way to do this with lambdas, so that it could fit on one line, but I can't wrap my head around it. I'd rather stay away from LINQ, but I'm not against it as a solution either.

string s = "A, B, C ,D";
string[] parts = s.Split(','); // This line should be able to perform the trims as well

I've been working in Python recently and I think that's what has made me revisit how I think about solutions to problems in C#.

5
  • 2
    Why stay away from LINQ? It's probably one of the most useful language features, and I think it's worth learning (or at least taking a peek). Commented Feb 25, 2013 at 19:14
  • Is there a reason you want to "stay away from LINQ"? Given how easy the LINQ solution is I suspect you are misunderstanding exactly what LINQ does. (e.g. the answers given below both use LINQ). Commented Feb 25, 2013 at 19:15
  • If the whitespace after the commas is consistent in your input string, then you only have to write string[] parts = s.Split(new[] { ", " }, StringSplitOptions.None); Commented Feb 25, 2013 at 19:18
  • The reason I want to stay away from LINQ is that I don't like not being able to necessarily know what the compiler is doing, but maybe that's because I don't understand what LINQ does under the covers well enough. For example, below is posted an answer: string[] parts = s.Split(',').Select(x => x.Trim()).ToArray(); This appears to create a new string array (the split function), but I don't know how the compiler implements the Select(x => x.Trim()) part. Does it make a copy? Does it run the function on each item on the return value of Split? Then when you call ToArray() does that make a copy? Commented Feb 27, 2013 at 1:36
  • I suppose I could write the function both ways and see what it compiles to, but would like to know what happens under the covers for the Select function. Commented Feb 27, 2013 at 1:38

3 Answers 3

8

What about:

string[] parts = s.Split(',').Select(x => x.Trim()).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

4
var parts = s.Split(',').Select(part => part.Trim());

3 Comments

I know the OP wrote it like that, but .Split(",") doesn't work. It expects a char instead of a string.
I wonder why this comment gets upvotes even if Servy has updated his answer immediately. @OscarMederos: You can delete it :)
@TimSchmelter Well, one of the upvotes was from me after I fixed it. One of the upvotes was before the edit, leaving only one other upvote after the fix.
1

If you really want to avoid LINQ, you can split on multiple characters and discard the extra "empty" entries you get between the "," and spaces. Note that you can end up getting odd results (e.g. if you have consecutive "," delimiters you won't get the empty string in between them anymore):

s.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

This will work for your sample input, but its very fragile. For example, as @Oscar points out, whitespace inside your tokens will cause them to get split as well. I'd highly recommend you go with one of the LINQ-based options instead.

1 Comment

This is what I usually do, but not for this scenario. As you said, it fails on A B, C D E, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.