开发者

C# Problem trimming line breaks within strings

开发者 https://www.devze.com 2023-02-18 13:05 出处:网络
I have开发者_如何学编程 a string that is populated from an Xceed datagrid, as such: study = row.Cells[\"STUDY_NAME\"].Value.ToString().TrimEnd(Environment.NewLine.ToCharArray());

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', ' ')
0

精彩评论

暂无评论...
验证码 换一张
取 消