A lot of references I come across have far too much info, so this post will give examples and brief descriptions of some of the most common and useful string-related methods. I tried to put the most common ones near the top, for quick reference.
Of course, most experienced developers will be quite familiar with many of these, but I think this is a good list for beginners to understand the range of methods available that can help accomplish complex operations using simple syntax.
Convert to String
You can convert a number, Boolean, or an object to a string:
Split a String Into Multiple Substrings
To separate a string into an array of substrings, you can use the split() method:
var wheresWaldo = stringOne.lastIndexOf("Waldo"); // 22
The lastIndexOf() method is exactly the same except it returns the starting position of the last occurrence of the passed in substring.
In both methods, if the substring is not found, the returned value is -1, and both allow an optional second argument indicating the position in the string where you want to start the search. So with a second argument of “5″, indexOf() starts searching at character 5, ignoring characters 0-4, whilelastIndexOf() starts searching at character 5 and goes in reverse, ignoring characters 6 and above.
Replace a Substring
To replace part or all of a string with a new string, you can use replace():
var stringTwo = stringOne.substring(5, 10); // "fghij"
For both slice() and substring(), the first argument is the character at which to begin the substring (again using zero-based numbering) and the second argument (which is optional) is the character in the string after you want the substring to end. So in the examples above, the arguments “5, 10″ mean characters 5 through 9 are “sliced” to create the new string.
var stringTwo = stringOne.substr(5, 10); // "fghijklmno"
For substr(), once again the first argument represents the character that begins the new string and the second argument is optional. But this time, the second argument represents the total number of characters that should be included, starting with the character in the “5″ position.
Convert a String to Uppercase or Lowercase
There are four methods that do case conversion. Two for converting a string to all uppercase:
var stringTwo = stringOne.toLocaleLowerCase(); // "you don't have to yell"
var stringThree = stringOne.toLowerCase(); // "you don't have to yell"
Generally, the results between the “locale” method and the non-locale method are the same, but according to MDN’s reference “for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.” Zakas’ book says “if you do not know the language in which the code will be running, it is safer to use the locale-specific methods.”
Matching a pattern within a string can be done using one of two methods, which basically work the same way.
The match() method is called on a string and is passed a regular expression:
As shown above, a negative number is returned if the string argument comes after the original string, a positive number is returned if the string argument comes before, and 0 is returned if the strings are equal.
Since a browser can return any negative number or any positive number for the before and after results (the spec doesn’t require anything specific), when checking the value, it’s best to use if ( result < 0 ), rather than if ( result === -1 ), the latter of which wouldn’t work in Chrome.