views:

37

answers:

1

In PHP, it's pretty simple, I'd assume, array_shift($string)?

If not, I'm sure there's some equally simple solution :)

However, is there any way to achieve the same thing in javascript?

My specific example is the pulling of window.location.hash from the address bar in order to dynamically load a specific AJAX page. If the hash was "2", i.e. http://foo.bar.com#2...

var hash = window.location.hash; // hash would be "#2"

I'd ideally like to take the # off, so a simple "2" gets fed into the function.

Thanks!

+5  A: 
hash = hash.substr(1);

This will take off the first character of hash and return everything else. This is actually similar in functionality to the PHP substr function, which is probably what you should be using to get substrings of strings in PHP rather than array_shift anyway (I didn't even know array_shift would work with strings!)

Dean Harding
...and `array = array.slice(1)` works similarly with arrays if curious...
gnarf
Cheers to the both of you :)As far as I know, "string" isn't exactly a real type, but rather an array of characters, as we've all been taught way way (way?) back when.Theoretically, it should work, as you can point to a character in a string with $string[x].
Julian H. Lam
I just tried it out, and `array_shift` actually *does not* work on strings in PHP, which makes sense to me. @Julian: what you say is true of strings in C and C++ but in most other languages (Java, C#, PHP, Python, etc), strings are their own data type mostly unrelated to arrays.
Dean Harding
Use `substring` instead of `substr`. The former is standardised.
J-P
Duly noted :) Thanks
Julian H. Lam