i need to get items from a List with no leading or trailing white spaces.i’m trying the following code but still Trim() functions does’t remove trailing spaces of the string.why does it happening?
string ab = string.Empty;
ab += "first" + ", ";//adding a white space to the string
ab += "second" + ", ";
ab += "third" + ", ";
List<string> ls = ab.ToString().Split(',').ToList();//first, second, third,
foreach (string item in ls)
{
item.Trim();//need to remove the space
string a = item;//here still got the white space
}
Trim returns a string that is trimmed of whitespace characters at the beginning and the end, so you’ll need to assign item.Trim() to a local variable, which will then be your trimmed string.