Javascript String Methods Flashcards
Convert a number, Boolean, or an object to a string
.toString()
var myNumber = 24; // 24
var myString = myNumber.toString(); // “24”
String()
var myNumber = 24; // 24
var myString = String(myNumber); // “24”
Split a String Into Multiple Substrings
.split()
var myString = “coming,apart,at,the,commas”;
var substringArray = myString.split(“,”); // [“coming”, “apart”, “at”, “the”, “commas”]
var arrayLimited = myString.split(“,”, 3); // [“coming”, “apart”, “at”]
Split a String Into Multiple Substrings while limiting the amount of splits you take
.split(“,”, 3)
var myString = “coming,apart,at,the,commas”;
var arrayLimited = myString.split(“,”, 3); // [“coming”, “apart”, “at”]
Get the Length of a String
.length
var myString = “You’re quite a character.”;
var stringLength = myString.length; // 25
Locate the first Substring within a String
.indexOf()
The indexOf() method starts searching for the substring (the first argument passed in) from the beginning of the string, and returns the position of the start of the first occurrence of the substring.
var stringOne = “Johnny Waldo Harrison Waldo”;
var wheresWaldo = stringOne.indexOf(“Waldo”); // 7
Locate the last Substring within a String
.lastIndexOf()
The lastIndexOf() method is exactly the same except it returns the starting position of the last occurrence of the passed in substring.
var stringOne = “Johnny Waldo Harrison Waldo”;
var wheresWaldo = stringOne.lastIndexOf(“Waldo”); // 22
Locate the first Substrig withing a String from a certain index
indexOf(“word”, 5)
starts searching at character 5, ignoring characters 0-4
Locate the last Substring within a String from a certain index
.lastIndexOf(“word”, 5)
starts searching at character 5 and goes in reverse, ignoring characters 6 and above.
Replace a Substring
.replace()
To replace part or all of a string with a new string, you can use replace(). The first argument is the substring you want to replace, and the second argument is the new substring. This will only replace the first instance of the matched substring.
var slugger = “Josh Hamilton”;
var betterSlugger = slugger.replace(“h Hamilton”, “e Bautista”);
console.log(betterSlugger); // “Jose Bautista”
Replace all instances of the matched substring
Use .replace() with a regular expression with the global flag
var myString = “She sells automotive shells on the automotive shore”;
var newString = myString.replace(/automotive/g, “sea”);
console.log(newString); // “She sells sea shells on the sea shore”
Find the Character at a Given Position
.charAt()
var myString = “Birds of a Feather”; var whatsAtSeven = myString.charAt(7); // “f”
.charCodeAt()
- gives you the character code, instead of the character itself.*
- var myString = “Birds of a Feather”;*
- var whatsAtSeven = myString.charCodeAt(7); // “102”*
- var whatsAtEleven = myString.charCodeAt(11); // “70”*
Concatenating Multiple Strings
.concat()
For the most part, when you concatenate strings, you’ll use the addition operator (+). But you do also have the option to use the concat() method:
var stringOne = “Knibb High football “;
var stringTwo = stringOne.concat(“rules.”); // “Knibb High football rules”
You can also pass multiple strings into it, and all will be appended (in the order they appear) to the original string:
var stringOne = “Knibb “; var stringTwo = “High “;
var stringThree = “football “; var stringFour = “rules.”;
var finalString = stringOne.concat(stringTwo, stringThree, stringFour);
console.log(finalString); // “Knibb High football rules.”
Slice a String (Extract a Substring)
.slice()
var stringOne = “abcdefghijklmnopqrstuvwxyz”;
var stringTwo = stringOne.slice(5, 10); // “fghij”
.substring()
var stringOne = “abcdefghijklmnopqrstuvwxyz”;
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.
Slice a String and take a set number of characters
.substr()
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
.toLocaleUpperCase() or .toUpperCase()
var stringOne = “Speak up, I can’t hear you.”;
var stringTwo = stringOne.toLocaleUpperCase(); // “SPEAK UP, I CAN’T HEAR YOU”
var stringThree = stringOne.toUpperCase(); // “SPEAK UP, I CAN’T HEAR YOU”
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.”