6

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.

4
  • is char 160 not a space in your encoding? or perhaps you are using the wrong code page?
    – Vlad
    Commented Jun 5, 2012 at 8:22
  • it's a right character i know. Commented Jun 5, 2012 at 8:32
  • this means that your codepage is wrong, because in cp1256 this character is a whitespace.
    – Vlad
    Commented Jun 5, 2012 at 8:34
  • based on your definition and of course cp1256, you're right. but my program has a MapTable for mapping that i'm right! :D Commented Jun 5, 2012 at 10:43

3 Answers 3

16

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.

5

Trim() removes leading and trailing white space characters, and that's excactly what it is supposed to do.

char 160 is a non breaking space, which is one of the white space characters it eliminates.

3

Trim removes all white-space, not just spaces. If char 160 is a whitespace in code page 1256, Trim will remove it.

The following code shows that 32 and 160 are whitespace in codepage 1256:

        var chars = new byte[] {32,160,164 };
        var enc=Encoding.GetEncoding(1256);
        var str=enc.GetString(chars);
        foreach (var character in str)
        {
            Console.WriteLine("{0}:{1}", character, Char.IsWhiteSpace(character));
        }
        Console.ReadKey();

Returns:

       True
       True
       False

Not the answer you're looking for? Browse other questions tagged or ask your own question.