Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Trim() removes more than need?

Tags:

string

c#

I found a ticket in our issue tracker that one of customers report a bug that one texts is incomplete! We have a text conversion program from one legacy system(IBM AS400) to a modern one. I tracked it and found an unknown behavior on my code!!

First see this: bug-full state

As you see, there is two char before first space (char32), but when i remove Trim(),the result is:

bug-free state

Yes, Trim() removes char160 from beginning! What happened that Trim() works more than need? Note: both pictures are captured in same test state.

like image 688
Meysam Javadi Avatar asked Jun 05 '12 08:06

Meysam Javadi


1 Answers

160 is a NBSP (Non-breaking space) and according to the documentation, Trim will remove all the whitespace. 160 is classified in Unicode as whitespace.

You might want to call Trim(' ') instead.

like image 176
leppie Avatar answered Sep 24 '22 17:09

leppie