Skip to content Skip to sidebar Skip to footer

Unexpected Result From String.indexof Function?

I am literally pulling my hair out on this one... Here's the situation. I have two javascript strings as follows: dsName = 'Test 1' replacementString = 'Test ' I'm trying to see i

Solution 1:

It looks like some of the characters that look like spaces are not actually simple spaces. Try this to see what the string really contains:

for (var i=0; i<replacementString.length; i++) 
    console.log(replacementString.charCodeAt(i));

You can replace non-breaking spaces by regular ones like this:

replacementString = replacementString.replace(String.fromCharCode(160), " ");

Solution 2:

Kudos to Wolfgang for getting me on the right path to figuring this out, but it turned out to be something completely unexpected and different...

I was pulling the value of replacementText from a <textarea> which had a style of white-space:nowrap. I guess when nowrap is turned on, it returns spaces as non-breaking (ASCII code 160) and not as regular spaces.

Here's a js-fiddle to see what's going on: http://jsfiddle.net/Jk9Cw/

What do you guys think? Is this a "duh you've should have known" or a "wow, that is something I've never run into before"?

Post a Comment for "Unexpected Result From String.indexof Function?"