Darren Young
Darren Young

Reputation: 11090

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.

Upvotes: 4

Views: 2081

Answers (3)

Andrew Orsich
Andrew Orsich

Reputation: 53685

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

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68707

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

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

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

Upvotes: 6

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60714

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

Upvotes: 5

Related Questions