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

8

5

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]

Wagram answered 15/10, 2008 at 16:0 Comment(0)
R
10

The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.

You could do this:

s = s.Trim();

However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.

Filling a new list:

List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();

temp.ForEach(delegate(string s) 
{ 
    listFromSplit.Add(s.Trim()); 
});

listFromSplit.Dump();

Populating Manually:

string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();

foreach (string s in temp)
{
    listFromSplit.Add(s.Trim()); 
};

listFromSplit.Dump();
Responsibility answered 15/10, 2008 at 16:9 Comment(1)
or do a for instead of foreachLineman
A
17

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.

Awhile answered 15/10, 2008 at 16:3 Comment(0)
R
10

The String.Trim() method returns a string representing the updated string. It does not update the string object itself, but rather creates a new one.

You could do this:

s = s.Trim();

However you cannot update a collection while enumerating through it so you'd want to either fill a new List while enumerating over the existing one or populate the List manually using the string array returned by String.Split.

Filling a new list:

List<string> temp = new List<string>("a, b".Split(",".ToCharArray()));
List<string> listFromSplit = new List<string>();

temp.ForEach(delegate(string s) 
{ 
    listFromSplit.Add(s.Trim()); 
});

listFromSplit.Dump();

Populating Manually:

string[] temp = "a, b".Split(",".ToCharArray());
List<string> listFromSplit = new List<string>();

foreach (string s in temp)
{
    listFromSplit.Add(s.Trim()); 
};

listFromSplit.Dump();
Responsibility answered 15/10, 2008 at 16:9 Comment(1)
or do a for instead of foreachLineman
C
4

Further to the answer posted by Adrian Kuhn you could do the following:

var result = listFromSplit.Select(s => s.Trim());
Cradle answered 25/2, 2009 at 1:12 Comment(0)
U
2

The string instances are immutable. Anything that seems to modify one, creates a new instance instead.

Uvular answered 15/10, 2008 at 16:6 Comment(0)
D
1

You are not assigning the trimmed result to anything. This is a classic error, I've only just got out of the habit of making this mistake with string.Replace :)

Dialectology answered 15/10, 2008 at 16:4 Comment(0)
V
1

I have no IDE up and running, but this should get the job done (unless I am wrong):

var result = from each in listFromSplit select each.Trim();
Voss answered 15/10, 2008 at 16:53 Comment(0)
C
0

Split on both spaces and commas and remove any empty entries. All nice and trimmed. Assumes that your strings don't contain spaces, though.

List<string> listFromSplit =
     new List<string>( "a , b ".Split( new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries ));
Cavein answered 15/10, 2008 at 16:9 Comment(0)
A
0

The linq options others have provided should work well. As another option, here is an extension method using a for loop:

    public static void TrimCollection(this IList<string> stringCollection) {

        for (int i = 0; i <= stringCollection.Count() - 1; i++)
            stringCollection[i] = stringCollection[i].Trim();

    }
Ayakoayala answered 10/8, 2012 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.