C# Trim() is not working in one of my projects

I have silly problem with Asp.Net project that I’m working on it for more than 5 years.
Today suddenly the Trim() function stopped working.

C# Trim() is not working in one of my projects


  • I update project framework from 4.7.2 to 4.8 and the problem still happen.
  • I tried TrimLeft() and TrimRight() also have the same problem of Trim()
  • Replace Function is still working fine not effected but it is not a good solution for me to use it every where.
  • Trim working fine on new projects
  • I also check the char code of space it is 32

any idea?

this is the code

    string val = "abo    ‎"; // "abox0020x0020x0020x0020x200e"
    string userName = val.Trim();

you can run the problem in this link

thanks you all for the comments, I also found a simple check to test the end of the char when you set the cursor at the end and press backspace one time nothing is happen the second press start deleting and that because of the x200e char at the end.

any idea how to trim hidden Char from left and right and deal with just like spaces.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

String.Trim works. If it didn’t, hundreds of thousands of developers would have noticed 16 years ago.

The string ends with a formatting character, specifically x200e, the Left-to-Right-Mark. That’s definitely not a whitespace. Calling Char.GetUnicodeCategory('‎') returns Format. I suspect the input came from mixed Arabic and Latin text, perhaps something copied by a user from a longer string?

One way to handle this is to use String.Trim(char[]) specifying the LTR mark along with other characters. That’s not quite the same as String.Trim() though, which removes any character that returns true with Char.IsWhiteSpace() :

var userName=val.Trim(' ','t','r','n','u200e`);

Another option would be to use a regular expression that trims both whitespace s and characters in the Format Unicode category Cf, only from the start ^[p{Cf}s] or end ([p{Cf}s]+$) :
string userName = Regex.Replace(val,@"(^[p{Cf}s]+)|([p{Cf}s]+$)","");

Perhaps a better option would be to prevent unexpected characters using input validation, and require that the input TextBox contains only letter or letter and digit characters. After all, the user could paste some other unexpected non-printable character. It’s better to warn the user than try to handle all possible bad data.

Usernames are typically letter and number combinations without whitespace. All ASP.NET stacks allow validation. Modern browsers allow regular expression validation in the input element directly, so we could come up with a regex that allows only valid characters, eg :

<input type="text" required pattern="[A-Z0-9]' ..../>

The NumberLetter block (Nl) could be used to capture numbers and letters in any language, just like Cf is used to capture format characters

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x