String.Trim () removes more than needed?

I found a ticket in our problem tracker that one of the clients reports an error that some texts are incomplete! We have a program for converting text from one old system (IBM AS400) to a modern one. I tracked it and discovered unknown behavior in my code.

First, let's see: bug-full state

As you can see, before the first space (char32) there are two char, but when I delete Trim(), the result:

bug-free state

Yes, Trim()removes char160 from the beginning! What happened, what Trim()works more than necessary? Note. both pictures are recorded in the same test state.

+5
source share
3 answers

160 - NBSP ( ), Trim . 160 Unicode .

Trim(' ').

+15

Trim() .

char 160 - , , .

+4

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

The following code shows that 32 and 160 are spaces in code page 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();

Return:

       True
       True
       False
+2
source

All Articles