Text Methods Flashcards
String.prototype.at()
The at() method of String values takes an integer value and returns a new String consisting of the single UTF-16 code unit located at the specified offset. This method allows for positive and negative integers. Negative integers count back from the last string character.
const sentence = 'The quick brown fox jumps over the lazy dog.'; let index = 5; console.log(`An index of ${index} returns the character ${sentence.at(index)}`); // Expected output: "An index of 5 returns the character u" index = -4; console.log(`An index of ${index} returns the character ${sentence.at(index)}`); // Expected output: "An index of -4 returns the character d"
String.prototype.charAt()
The charAt() method of String values returns a new string consisting of the single UTF-16 code unit at the given index.
charAt() always indexes the string as a sequence of UTF-16 code units, so it may return lone surrogates. To get the full Unicode code point at the given index, use String.prototype.codePointAt() and String.fromCodePoint().
const sentence = 'The quick brown fox jumps over the lazy dog.'; const index = 4; console.log(`The character at index ${index} is ${sentence.charAt(index)}`); // Expected output: "The character at index 4 is q"
String.prototype.endsWith()
The endsWith() method of String values determines whether a string ends with the characters of this string, returning true or false as appropriate.
const str1 = 'Cats are the best!'; console.log(str1.endsWith('best!')); // Expected output: true console.log(str1.endsWith('best', 17)); // Expected output: true const str2 = 'Is this a question?'; console.log(str2.endsWith('question')); // Expected output: false
String.prototype.indexOf()
The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.
const paragraph = "I think Ruth's dog is cuter than your dog!"; const searchTerm = 'dog'; const indexOfFirst = paragraph.indexOf(searchTerm); console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`); // Expected output: "The index of the first "dog" is 15" console.log( `The index of the second "${searchTerm}" is ${paragraph.indexOf( searchTerm, indexOfFirst + 1, )}`, ); // Expected output: "The index of the second "dog" is 38"
String.prototype.lastIndexOf()
The lastIndexOf() method of String values searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.
const paragraph = "I think Ruth's dog is cuter than your dog!"; const searchTerm = 'dog'; console.log( `Index of the last ${searchTerm} is ${paragraph.lastIndexOf(searchTerm)}`, ); // Expected output: "Index of the last "dog" is 38"
String.prototype.match()
The match() method of String values retrieves the result of matching this string against a regular expression.
const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.'; const regex = /[A-Z]/g; const found = paragraph.match(regex); console.log(found); // Expected output: Array ["T", "I"]
String.prototype.matchAll()
The matchAll() method of String values returns an iterator of all results matching this string against a regular expression, including capturing groups.
const regexp = /t(e)(st(\d?))/g; const str = 'test1test2'; const array = [...str.matchAll(regexp)]; console.log(array[0]); // Expected output: Array ["test1", "e", "st1", "1"] console.log(array[1]); // Expected output: Array ["test2", "e", "st2", "2"]
String.prototype.padEnd()
The padEnd() method of String values pads this string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of this string.
const str1 = 'Breaded Mushrooms'; console.log(str1.padEnd(25, '.')); // Expected output: "Breaded Mushrooms........" const str2 = '200'; console.log(str2.padEnd(5)); // Expected output: "200 "
String.prototype.padStart()
The padStart() method of String values pads this string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of this string.
const str1 = '5'; console.log(str1.padStart(2, '0')); // Expected output: "05" const fullNumber = '2034399002125581'; const last4Digits = fullNumber.slice(-4); const maskedNumber = last4Digits.padStart(fullNumber.length, '*'); console.log(maskedNumber); // Expected output: "************5581"
String.prototype.repeat()
The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together.
const mood = 'Happy! '; console.log(`I feel ${mood.repeat(3)}`); // Expected output: "I feel Happy! Happy! Happy! "
String.prototype.replace()
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.
const paragraph = "I think Ruth's dog is cuter than your dog!"; console.log(paragraph.replace("Ruth's", 'my')); // Expected output: "I think my dog is cuter than your dog!" const regex = /Dog/i; console.log(paragraph.replace(regex, 'ferret')); // Expected output: "I think Ruth's ferret is cuter than your dog!"
String.prototype.replaceAll()
The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
const paragraph = "I think Ruth's dog is cuter than your dog!"; console.log(paragraph.replaceAll('dog', 'monkey')); // Expected output: "I think Ruth's monkey is cuter than your monkey!" // Global flag required when calling replaceAll with regex const regex = /Dog/gi; console.log(paragraph.replaceAll(regex, 'ferret')); // Expected output: "I think Ruth's ferret is cuter than your ferret!"
String.prototype.search()
The search() method of String values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.
const paragraph = "I think Ruth's dog is cuter than your dog!"; // Anything not a word character, whitespace or apostrophe const regex = /[^\w\s']/g; console.log(paragraph.search(regex)); // Expected output: 41 console.log(paragraph[paragraph.search(regex)]); // Expected output: "!"
String.prototype.slice()
The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.
const str = 'The quick brown fox jumps over the lazy dog.'; console.log(str.slice(31)); // Expected output: "the lazy dog." console.log(str.slice(4, 19)); // Expected output: "quick brown fox" console.log(str.slice(-4)); // Expected output: "dog." console.log(str.slice(-9, -5)); // Expected output: "lazy" ****
String.prototype.split()
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
const str = 'The quick brown fox jumps over the lazy dog.'; const words = str.split(' '); console.log(words[3]); // Expected output: "fox" const chars = str.split(''); console.log(chars[8]); // Expected output: "k" const strCopy = str.split(); console.log(strCopy); // Expected output: Array ["The quick brown fox jumps over the lazy dog."]