Getting Good with JavaScript - Andrew Burgess [15]
Example 3.11
var line = "HAL: I'm sorry, Dave. I'm afraid I can't do that";
alert( line.indexOf("I'm") ); // 5
alert( line.indexOf("I'm", 6) ); // 22
You can pass a second parameter as the starting point, if you don't want to start searching at the beginning of your string. Remember, these indices are zero-based—just like the indices of an array—which means that the first letter of the string is indexed 0, the second is 1, and so on. If the substring you searched for doesn't exist, you'll get -1 back.
slice, substr, and substring
That's right; there are three methods for getting part of a string out of a larger string. First, you can use slice. It takes two parameters: the first parameter is the starting index, and the second one is the ending index, meaning the index of the character after the last character in the desired substring. If you leave the second one off, it will slice until the end of the string.
Example 3.12
var greeting = "Hello, Andrew, what's up?",
name = greeting.slice(7, 13);
alert(name); // Andrew
These index parameters can also be negative numbers, which means they "count" from the end of the string. This way, -1 is the last item in the string, -2 is the second last, and so on.
An alternative to slice is substr. The first parameter is the same as slice—the starting index—but the second parameter is the length of the substring:
Example 3.13
var greeting = "Hello, Andrew, what's up?",
name = greeting.substr(7, 6);
alert(name); // Andrew
Since you're string can't have a negative length, you can't use a negative number for the second parameter (you can use a negative number for the first parameter, though). Of course, that second parameter is optional, if you want the rest of the string.
Finally, there's substring. This works similarly to slice, except when it comes to negative values. A negative value for either parameter acts as 0—it refers to the start of the string. The neat thing about substring is that, unlike slice, the second parameter can be lower than the first (while still positive). When this is the case, substring goes back to the character of that index. For example,
Example 3.14
var greeting = "Hello, Andrew, what's up?",
name = greeting.substring(13, 7);
alert(name); // Andrew
split
It's easy to pull a string into an array with the split method. Just pass the method a parameter determining what character to split the array on.
Example 3.15
var arr = "apples oranges peaches bananas".split(" ");
console.log(arr); // ["apples", "oranges", "peaches", "bananas"]
Yes, this is the way lazy programmers create arrays. But it has more use than just as an alternative to an array literal.
toLowerCase and toUpperCase
I'm sure you know exactly what these do: easy converting of a string to upper- or lowercase:
Example 3.16
var usa = "usa".toUpperCase(),
comment = "THIS MIGHT BE A COMMENT";
comment = comment.substr(0,1) + comment.slice(1).toLowerCase();
console.log(usa); // USA
console.log(comment); // This might be a comment
Numbers
toExponential
If you'd like to convert a number to its exponential form, this is the method to use. It takes one parameter: the number of decimals places to use. If you leave the parameter out, JavaScript will make sure your number isn't rounded.
Example 3.17
alert( 12345..toExponential() ); // "1.2345e+4", meaning 1.2345 x 10^4
alert( 987.65432.toExponential(3) ); // "9.877e+2"
alert( 0.1234.toExponential() ); // "1.234e-1"
If your scratching your head at the double-period in the first example above, a short explanation is in order. When JavaScript sees a dot after a number, it expects more digits; it thinks it's a decimal point. So writing 5 is translated as 5.0. So if we wrote 5.toExponential(), we'd get an error. We could do 5.0.toEponential() if we wanted to, or you could save the character and just do the double-dot. Don't worry, in most cases, your numbers will probably be in variables, so it won't make to much of a difference. (You could also wrap the number in parentheses: