Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Problem trimming line breaks within strings

Tags:

string

c#

trim

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.

like image 343
Darren Young Avatar asked Dec 22 '22 16:12

Darren Young


2 Answers

TrimEnd('\r', '\n');

isn't working because you have a space at the end of that string, use

TrimEnd('\r', '\n', ' ');
like image 95
Yuriy Faktorovich Avatar answered Dec 24 '22 04:12

Yuriy Faktorovich


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' );

like image 37
Øyvind Bråthen Avatar answered Dec 24 '22 04:12

Øyvind Bråthen