Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my attempt to trim strings in a List<string> not appear to work?

I tried the following code in LINQPad and got the results given below:

List<string> listFromSplit = new List<string>("a, b".Split(",".ToCharArray())).Dump();
listFromSplit.ForEach(delegate(string s) 
{ 
  s.Trim(); 
});
listFromSplit.Dump();

"a" and " b"

so the letter b didn't get the white-space removed as I was expecting...?

Anyone have any ideas

[NOTE: the .Dump() method is an extension menthod in LINQPad that prints out the contents of any object in a nice intelligently formatted way]

like image 255
rohancragg Avatar asked Oct 15 '08 16:10

rohancragg


1 Answers

you're just creating a trimmed string, not assigning anything to it.

var s = "  asd   ";
s.Trim();

won't update s, while..

var s = "   asd   ";
s = s.Trim();

will..

var listFromSplit = "a, b".Split(',').Select(s=>s.Trim());

would, i suppose, be how i'd go about it.

like image 125
Sciolist Avatar answered Sep 18 '22 06:09

Sciolist