Strings are, as you likely know, text. More than that, you can think of strings as arrays, who's cells store one letter each. This mindset will help you in the coming lesson. So basically, the string "Hello world" is an array with 11 elements. Element 0 stores the letter "H", element 5 stores the " ", and element 10 stores the letter "d".
First off, we'll look at a few functions that help you find the position of a letter, or find the letter that is in a given position. They are indexOf() and charAt(). They both are tacked onto a variable that contains a string, such as my_string.indexOf("h").
indexOf() takes the letters in the parentheses and searches the string to which it is attached. If it finds a match, it returns the index of the first occurance of that match. If it doesn't, it returns -1. Here are some examples.
var my_string = "This is a string of text";
var string_portion = "of";
var is = my_string.indexOf("is");
var e = my_string.indexOf("e");
var of = my_string.indexOf(string_portion);
So variable is would equal 2; it found the first occurance of "is" in my_string, which is the end of "This". Variable e would equal 21. You don't have to set it equal to something in order to use it though, as I'll show you a bit later. Lastly, variable "of" equals 17.
Next is charAt(). It finds the character at the given index. This is an absurdly simple one, with one exception. Be careful to ensure that the string is big enough for the number you try to find. If you don't you'll get an out of index error and it'll mess everything up.
var my_string = "This is another string of text";
var seven = my_string.charAt(7);
var three = my_string.charAt(3);
var redundant = my_string.charAt(my_string.indexOf("e"));
Variable seven would equal " ", which is the seventh index of the string. Variable three would equal "s", and variable redundant would equal the character at the index of the letter e, which is e! my_string.indexOf("e") = 13, and charAt(13) = e. Now you know why I called it redundant - but at least it shows how you can use indexOf() as an integer.
Onto a more powerful function - substring. This takes a string, and takes a portion of that string and stores it into another variable or whatever you want to do with it. Attached to the end of a variable which contains a string like the other functions, this one has two parameters. The first one is the index of the beginning of the string you want to create, and the second one is the actual position (counting from 1) of the last letter. Example:
var my_string = "This is yet another string of text";
var my_substring = my_string.substring(8,19);
var next_substring = my_substring.substring
(my_substring.indexOf(" ") + 1,my_substring.length);
Note: The text wraps here, but it shouldn't in your code! Your first true peek at the true power of JavaScript's string handling abilities. The first substring makes "yet another", and the second one starts with one more than the index of the space, or the "a", and ends at the length of my_substring, which is the actual length; the length variable counts from one. In the end it makes "another". Ain't it cool? However, the previous operation is rednered pretty much obsolete by the next function.
Now onto the split() function. It does what it says; it splits the string based on what is in the parenthesis, and stores the results into an array. This is easiest to show you by an example:
var my_string = "Here lies another string of text";
var split_string = my_string.split(" ");
This splits the first string into 6 different elements, each of which contains one word of the original string, and stores it all into split_string which is turned into an array by the split function. Also note that it does not include the parameter used to make the split in any of the cells - it deletes the element used to make the split. So split_string[0] equals Here, split_string[1] equals lies, split_string[3] equals string, and so on. A truly valuable function, to say the least.
That's it for JavaScript's string handling abilities. But when combined, they are unspeakably powerful.
Now onto Associative Arrays. You've heard of them before, but I really only glazed over them. They, quite simply, store information in words, not numbers. Again, it's easiest to lead by example:
var word_array = new Array(); word_array["element_one"] = "Here lies the first element"; word_array["element_two"] = "Here lies the second element"; word_array["my_name"] = "Ryan Boyer";
As you can see, instead of the elements being stored as indexes, they are stored under names. So whenever you use word_array["my_name"], it'll act as "Ryan Boyer". There needn't be any rhyme or reason to your naming scheme, and there are some unexpected uses of these arrays in the next lesson.