3

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 if dsName starts with replacementString, with the following code:

if(dsName.indexOf(replacementString) == 0)
{
    // I never get here!
}

indexOf is returning -1!! How is this possible? I can put a breakpoint in Chrome script debugging right before that line and paste "dsName.indexOf(replacementString)" into the console and see that it is indeed returning -1.

Now just to prove I'm not crazy I can from that same breakpoint print out dsName and it does in fact equal "Test 1" and replacementString does equal "Test ". Here is an actual screenshot from the Chrome debugging console:

enter image description here

So as you can see, if I paste in the literal string, it works as expected, but if I use the variable, it doesn't work. I've even tried String(replacementString) and replacementString.toString() to see if maybe it was a type issue, but it does the same thing.

It's like it works if the parameter for indexOf is a literal string, but not if it's a string variable.

Am I going crazy, is there a something stupid I'm missing? Or is this possibly a bug in Chrome?

16
  • 1
    Of course it works jsfiddle.net/mplungjan/g6sGa - Chrome OSX Commented Jan 17, 2014 at 20:52
  • I just tried this in Chrome v. 31.0.1650.63, and it worked just fine. Commented Jan 17, 2014 at 20:52
  • What does replacementString.chatCodeAt(0) give you Commented Jan 17, 2014 at 20:53
  • 2
    It is a possibility that there is a non-printable character in one of you strings. Its works Commented Jan 17, 2014 at 20:53
  • No repro in 33.0.1750.18 Commented Jan 17, 2014 at 20:53

2 Answers 2

6

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), " ");
Sign up to request clarification or add additional context in comments.

4 Comments

See stackoverflow.com/posts/comments/31916157?noredirect=1. Clearly, the characters are spaces. (edit: direct link)
So it appears you might be right: replacementString.charCodeAt(4) = 160 and dsName.charCodeAt(4) = 32...
Need to figure out what is causing that... So it looks like the char in replacement string is a non-breaking space...
Is your value perhaps posted from a browser or taken from a DOM node? You could replace all non breaking spaces by regular ones using String.fromCharCode(160).
1

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"?

1 Comment

Interesting. I can see how this could make some people go bald.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.