0

I found another question to help me find out if a string starts with another string here. I used Mark Byers answer.

However, I ran into a problem using this. For some reason it's not finding the string and returning -1.

var response = doWork();

alert(response.lastIndexOf('/apex', 0));
if (response.lastIndexOf('/apex', 0) === 0)
  window.location.href = response;
else
  alert('test' + response + 'test');

The comparison returns -1, but the alert at the end returns test/apex/IFE__IFE_ViewQuote?Id=a01i000000XobwRAARtest. Am I doing something wrong?

I added a test to fire AFTER the above:

var response2 = '/apex/IFE__IFE_ViewQuote?Id=a01i000000Xoc4tAAB'; 
alert('index = ' + response2.lastIndexOf('/apex', 0));

This returned "index = 0".

Community
  • 1
  • 1
dphil
  • 133
  • 8
  • Seems to work fine for me http://jsfiddle.net/hmzpsxtw/ – James Montagne May 22 '15 at 20:01
  • I suspect there's a space at the beginning of `response`. – Barmar May 22 '15 at 20:01
  • I just changed the alert at the end alert('test' + response + 'test'); and there seems to be no spaces in it. Hmmmm this is the strangest thing. – dphil May 22 '15 at 20:12
  • Could you paste the result of `alert(btoa(response));` please? – Siguza May 22 '15 at 20:25
  • @Siguza L2FwZXgvSUZFX19JRkVfVmlld1F1b3RlP0lkPWEwMWkwMDAwMDBYb2M3SkFBUg== – dphil May 22 '15 at 20:26
  • It's possible that the literal `'/apex'` in `if (response.lastIndexOf('/apex', 0) === 0)` has an invisible garbage character. Have you tried re-entering that line of code? – Ted Hopp May 22 '15 at 20:32
  • @TedHopp That is very possible. How might I get rid of that garbage character? The text is generated as a response from a REST service. What do you mean by re-entering that line of code? Do you mean by hardcoding it? When I hardcode the string, it seems to work. – dphil May 22 '15 at 20:36
  • I meant that the garbage character was in the code, not in the response. I was suggesting that you re-type the `if` statement. The `btoa(response)` pretty much rules out a garbage character in `response`. – Ted Hopp May 22 '15 at 20:39
  • @TedHopp I manually typed out the if statement by hand instead of copy/paste and it still does the same thing. I imagine it could be a hidden character somehow in the string returned though. – dphil May 22 '15 at 20:47
  • @dphil The `btoa` string rules that out, that's why I wanted to see it. `response` is clean. – Siguza May 22 '15 at 20:49
  • 1
    That would have been revealed by the base-64 encoding that Siguza asked you to post. Assuming that the code you posted is the code that's running, this behavior is a mystery. – Ted Hopp May 22 '15 at 20:49
  • Gotcha... Well crap. – dphil May 22 '15 at 20:49
  • Could `response` be an array with one element? That would explain everything. – Siguza May 22 '15 at 20:57
  • Must be code leprechauns as I've not been able to duplicate the behavior. – Yogi May 22 '15 at 20:58
  • I think I figured out a workaround using btoa. Will the encryption from that ever change? – dphil May 22 '15 at 21:05

1 Answers1

0

So, I guess I figured out a solution that HOPEFULLY will work for all cases. I used btoa to figure out what '/apex' would be and took the characters that matched between this and btoa of the response. They matched up to 'L2FwZXg' (there was an extra = at the end). This seemed to work.

if (btoa(response).lastIndexOf('L2FwZXg', 0) === 0)
dphil
  • 133
  • 8
  • Still, that doesn't explain anything. I suspect that `response` is an array with one element, which is the string. Array to string conversion apparently means `array.join(',')`, and with one element only, that would look like it was a string when base64-encoded or `alert`ed. But could you please try `alert(JSON.stringify(response));` and tell me whether there are `[]` around the string or not? – Siguza May 22 '15 at 22:35