|Lesson 17&18: Querys and Strings|
var myString='A group of numbers & letters';
You can reference individual characters in the string by their order in the string starting from zero, so the letter g in myString would be the 2 character (A=0,
You can reference portions of the string by creating a substring with the first desired character's position and the last desired characters's position+1, so the word "numbers" in myString would be-
Conversly, you can find the reference index of the first or last instance of a characther, so the index of "p" in myString would be-
and the last occurance of t would be-
You can also use slice() and substr() in similar ways with various rules for the starting and stopping positions. And finally, use the length property to find the number of characters in the string-
Perhaps the most powerful method of extracting data from strings programmatically is the split() method. This takes a string and "splits" it into substrings using the specified character as the splitting point, and places the new substrings in an array. In the example myString, we could split the string at the spaces to return an array of all the words in the sentence-
Which would return an array like this:
If instead you split it at the letter "n"-
you would get-
mySplitString=='A group of '
mySplitString=='umbers & letters'
One special string we can use these methods on to create interactive pages is the query string of the location or url of the page. The query string you'll remember from previous discussions is the portion of the url that starts at the question mark (?) and continues till the end or a hash mark (#) Example-
By splitting out these values from the query string of the url, we can load a single page with unique data each time it is loaded.
We split off the question mark with-
We split the pairs apart at the ampersand (&)-
Finally, we split these pairs apart giving us useable data-
Now we have useable data, passed from another page, broken down into it's basic portions, ready to be used or passed to another page. Here is a basic script that will create a new object named "queryInfo" containing the name/value pairs of all data transmitted via form url encoding and writes them out in a table (you can output or use the data in any fashion):
You can use this script to check the functionality of your forms, or modify it for any use!
http://webwizardsways.com/experiment/query.html (put a question mark and info on it to test)
Bonus String Example- Simple Scroller
Here is a basic scroller that shows how you can grab single characters out of strings: