JS - String Methods

20 flashcards · Programming Languages · by zeckdude

  1. 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"

  2. 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

  3. 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."

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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 "

  10. 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"

  11. 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

  12. 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"

  13. 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"]

  14. 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

  15. 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"

  16. 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"

  17. 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"

  18. 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!"

  19. 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 "

  20. 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"