Skip to content

The best String.trim()…

by chris on June 2nd, 2010

…trims characters that aren’t even there!

I was having a strange bug (while developing the message/user polling for my MooTools chat class) where trimming wasn’t stripping the whitespace in the returned data. You see, unlike many good client-server apps, I am not using JSON, but rather a specialized CSV format. The regular expression methods I tried didn’t work.

The character matching/traversal methods I tried didn’t work. And then I found out that the character code for the whitespace characters that were popping up were 65279. Sixty seconds later (thanks to Google!) I found this article. So my trim method now looks something like this:

Did I mention it happens to be very fast? :)

From → Javascript

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS