JS - String Methods
20 flashcards · Programming Languages · by zeckdude
What is the string method to return a character at a specified index? Provide example using the supplied code: str = 'Here is some example text!'
str.charAt(3); // returns "e"
What is the string method to return a unicode for the character at a specified index? Provide example using the supplied code: str = 'Here is some example text!'
str.charCodeAt(3); // returns 101
What is the string method to return two concatenated strings? Provide example using the supplied code: str = 'Here is some example text!' str2 = 'Feast your eyes.'
str.concat(str2) // returns "Here is some example text!Feast your eyes."
What is the string method to determine if a string ends with a specified substring? Provide example using the supplied code: str = 'Here is some example text'
str.endsWith("text") // returns true
What is the string method to determine if a string contains a specified substring? Provide example using the supplied code: str = 'Here is some example text'
str.includes('some') // returns true
What is the string method to return the index of the first occurrence of a substring within a string? Note: This method does not accept regular expressions. Provide example using the supplied code: str = 'Here is some example text!'
str.indexOf('ex'); // returns 13
What is the string method to return the index of the last occurrence of a substring within a string? Provide example using the supplied code: str = 'Here is some example text!'
str.lastIndexOf('ex'); // returns 22
What is the string method to return all the matches of a substring within a string as an array? Provide example using the supplied code: str = 'Here is some example text!'
str.match(/ex/g) // returns ['ex', 'ex'] // Without the `g` identifier, it will only return the first found match
What is the string method to return a repeated string? Provide example using the supplied code: str = 'Hello there '
str.repeat(3) // returns "Hello there Hello there Hello there "
What is the string method to return a string that replaces all the matches of a substring, within a string, with another substring? Provide example using the supplied code: str = 'Here is some example text!'
str.replace(/ex/g, 'EX') // returns "Here is some EXample tEXt"
What is the string method to return the index of the first occurrence of a substring within a string? Note: This method accepts regular expressions and strings. Provide example using the supplied code: str = 'Here is some example text!'
str.search(/ex/) // returns 13
What is the string method to extract a substring from a string and return it as a new string? Hint: There are two methods that can be used to solve this. Provide both approaches in the example. Note: This method is used when the begin and end indices are known. Provide example using the supplied code: str = 'Here is some example text!'
str.slice(8, 12) // returns "some" str.substring(8, 12) // returns "some"
What is the string method to split a string as elements of a returned array? Provide example using the supplied code: str = 'Here is some example text!'
str.split(' ') // returns ["Here", "is", "some", "example", "text"]
What is the string method to determine if a string begins with a specified substring? Provide example using the supplied code: str = 'Here is some example text'
str.startsWith('Here') // returns true
What is the string method to extract a substring from a string and return it as a new string? Note: This method is used when the begin index and number of characters is known. Provide example using the supplied code: str = 'Here is some example text!'
str.substr(8, 4) // returns "some"
What is the string method to return a string in all lowercase characters? Provide example using the supplied code: str = 'Here is some example text!'
str.toLowerCase() // returns "here is some example text"
What is the string method to return a string in all uppercase characters? Provide example using the supplied code: str = 'Here is some example text!'
str.toUpperCase() // returns "HERE IS SOME EXAMPLE TEXT"
What is the string method to return a string with whitespace removed from the beginning and end of it? Provide example using the supplied code: str = ' Oh Yeah! '
str.trim() // returns "Oh Yeah!"
What is the string method to return a string that pads the end of the current string with a given string (repeated, if needed) so that the resulting string reaches a given length? Provide example using the supplied code: str = 'Hello' Pad the end of the string to make it 7 characters with the word I can write a long sentence Pad the end of the string to make it 10 characters with the word man Pad the end of the string to make it 10 characters with empty spaces
str.padEnd(10, 'I can write a long sentence') // returns "HelloI can" str.padEnd(10, 'man') // returns "Hellomanma" str.padEnd(10) // returns "Hello "
What is the string method to return a string that pads the beginning of the current string with a given string (repeated, if needed) so that the resulting string reaches a given length? Provide example using the supplied code: str = 'Hello' Pad the beginning of the string to make it 7 characters with the word I can write a long sentence Pad the beginning of the string to make it 10 characters with the word man Pad the beginning of the string to make it 10 characters with empty spaces
str.padStart(10, 'I can write a long sentence') // returns "I canHello" str.padStart(10, 'man') // returns "manmaHello" str.padStart(10) // returns " Hello"