length property of a string is also not considered a method.
IndexOf() and LastIndexOf()
indexOf() method returns the position within a string where the first occurrence of a specified substring can be found. The
lastIndexOf() method does the same thing, finding the last occurrence of a specified substring rather than the first. Both methods return -1 if the substring cannot be found.
let example1a = "abcdabc"; let example1b = example1a.indexOf("abc"); // 0 let example1c = example1a.lastIndexOf("abc"); // 4
Both of these methods also take a second parameter that identifies where the string search should start. The
indexOf() method traverses the string from the beginning, while the
lastIndexOf() method traverses the string from the end.
let example1a = "abcdabc"; let example1d = example1a.indexOf("abc", 3); // 4 let example1e = example1a.lastIndexOf("abc", 3); // 0
search() method returns the position within a string where the first occurrence of a specified substring can be found, similar to the
indexOf() method. The
search() method can find regular expression matches.
let example1a = "abcdabc"; let example1f = example1a.search("a..d"); // 0
CharAt() and CharCodeAt()
charAt() method returns the character at a specific position in a string, while the
charCodeAt() method returns the UTF-16 character code corresponding to the character at a specific string position. The UTF-16 character code is always between 0 and 65535.
let example2a = "Hi there!"; let example2b = example2a.charAt(0); // H let example2c = example2a.charCodeAt(0); // 72
slice() method extracts part of a given string and returns that substring.
It requires one parameter: the start position of the substring. The substring includes the start position and the rest of the string after it. Alternatively, the
slice() method can take a second parameter specifying where the substring should stop. The substring does not include the specified stop index.
Either parameter to the
slice() method can be positive or negative. Positive parameters count forward from the first character in the string, while negative parameters count backward from the last character in the string.
let example3a = "abcdefghijklmnopqrstuvwxyz"; let example3b = example3a.slice(5); // "fghijklmnopqrstuvwxyz" let example3c = example3a.slice(-10); // "qrstuvwxyz" let example3d = example3a.slice(5, 8); // "fgh" let example3e = example3a.slice(-10, -6); // "qrst"
substring() method works identically to the
slice() method except that it cannot take negative string indexes as parameters (all “distances” are measured from the front of the string).
let example3a = "abcdefghijklmnopqrstuvwxyz"; let example3f = example3a.substring(5); // "fghijklmnopqrstuvwxyz" let example3g = example3a.substring(5, 8); // "fgh"
substr() method works identically to the slice() method except that its second parameter specifies the substring length, rather than the stop index position.
let example3a = "abcdefghijklmnopqrstuvwxyz"; let example3h = example3a.substr(5); // "fghijklmnopqrstuvwxyz" let example3i = example3a.substr(-10); // "qrstuvwxyz" let example3j = example3a.slice(5, 8); // "fghijklm" let example3k = example3a.slice(-10, -6); // "opqrst"
replace() method finds a substring and replaces it with another. The replacement is case sensitive. In this example, the first call to
replace() would not be successful, but the second call would.
let example4a = "Please visit the UDY blog!"; let example4b = example4a.replace("UdY", "Udacity"); let example4c = example4a.replace("UDY", "Udacity");
ToUpperCase() and ToLowerCase()
toLowerCase() methods convert a string into uppercase and lowercase, respectively.
let example5a = "Udacity"; let example5b = example5a.toUpperCase(); // "UDACITY" let example5c = example5a.toLowerCase(); // "udacity"
split() method converts a string into an array. If you do not pass a delimiter string to this method, it will return an array that contains the entire string in the first array index. An empty string delimiter causes this method to return an array of single characters.
let example5a = "Udacity"; let example5e = example5a.split(); // ["Udacity"] let example5f = example5a.split(""); // ["U","d","a","c","i","t","y"] let example5g = example5a.split("a"); // ["Ud","city"]
trim() method removes leading and trailing whitespace from a string.
let example6a = " Welcome to Udacity! "; let example6b = example6a.trim(); // "Welcome to Udacity!"
PadStart() and PadEnd()
padEnd() methods pad the beginning or end of a string, respectively, by adding a specified number of characters onto the string until the string reaches the desired length. The first parameter is the desired string length, and the second parameter is the padding character.
let example7a = "1"; let example7b = example7a.padStart(4,"a"); // "aaa4" let example7c = example7a.padEnd(5, "b"); // "5bbbb"
padEnd() methods are the most recent additions to the string method lexicon.