C# Problem trimming line breaks within strings
I have开发者_如何学编程 a string that is populated from an Xceed datagrid, as such:
study = row.Cells["STUDY_NAME"].Value.ToString().TrimEnd (Environment.NewLine.ToCharArray());
However, when I pass the study string into another part of the program, it throws an error as the string is still showing as : "PI3K1003\n "
I have also tried:
TrimEnd('\r', '\n');
Anybody any ideas?
Thanks.
TrimEnd('\r', '\n');
isn't working because you have a space at the end of that string, use
TrimEnd('\r', '\n', ' ');
The problem in that string is that you do not have a \n
at the end. You have a ' '
(blank) at the end, and therefore \n
does not get trimmed.
So I would try TrimEnd( ' ', '\n', '\r' );
Use replace instead of TrimEnd, in this case you never run into a 'space problem' problem ;)
string val = @"PI3K1003\n";
val = val.Replace(@"\n", String.Empty).Replace(@"\r", String.Empty);
But TrimEnd is always solution:
TrimEnd('\r', '\n', ' ')
精彩评论