Search code examples
c#stringreplacetrimremoving-whitespace

Trim Not Removing Multiple White Spaces


I have the following very simple example;

 string MyString = "Egyptian Soccer/Egyptian Premier/Fixtures 20 January  /El Shorta v El Geish";

 string[] description = MyString.Split('/');

 description.Select(s => s.Trim());

However the string value of 'Fixtures 20 January ', is not having the whitespaces removed at the end.

What am I doing wrong ?


Solution

  • You need to reassign the result of the Select operation

    string MyString = "Egyptian Soccer/Egyptian Premier/Fixtures 20 January  /El Shorta v El Geish";
    
    string[] description = MyString.Split('/');
    var result = description.Select(s => s.Trim());
    foreach(string s in result)
        Console.WriteLine("|" + s + "|");