returns the number of characters that the string contains.
when you want to find the number of characters that the string contains
const str = 'this string has 29 characters'
console.log(str.length) // 29
removes white spaces from the beginning and end of a string
when processing the string of a user input field. It is easy to accidentally add spaces, and this ensures you handle the relevant characters.
const str = ' the secret to life is 42 '
console.log(str.trim()) // "the secret to life is 42"
determines if a substring is contained in a larger string and returns true or false
string matching for searching/parsing
const author = 'George Orwell'
const userSearchTerm1 = 'George'
const userSearchTerm2 = 'Douglas'
console.log(author.includes(userSearchTerm1)) // true
console.log(author.includes(userSearchTerm2)) // false
returns the index of the substring within the string. If the substring is not contained in the original string, it will return -1.
when you want to find the index of a substring
const author = 'George Orwell'
const userSearchTerm1 = 'Orwell'
const userSearchTerm2 = 'Douglas'
console.log(author.indexOf(userSearchTerm1)) // 7
console.log(author.indexOf(userSearchTerm2)) // -1
returns a string with all upper case letters
when you want to capitalize all the letters in a string
const str = 'We are some small letters that want to be big'
console.log(str.toUpperCase()) // 'WE ARE SOME SMALL LETTERS THAT WANT TO BE BIG"
returns a string with all lower case letters
when you want to return all the letters of a string to lowercase
const str = 'This has BIG LETTERS. They want to all be small.'
console.log(str.toLowerCase()) // 'this has big letters. they all want to all be small.'
returns a string with a pattern replaced by a replacement string. It takes either a regex or a string as the pattern. With a regex you can globally replace all matches (using the g option), but with a string it will only replace the first occurrence. In the example below, you will notice that world is replaced only once in the first call since it uses a string pattern.
When you want to replace a string with a new value
extracts a section of a string based on the index supplied and return it as a new string.
when you know the structure of a sting and want to retrieve a certain portion
takes a separator which you want to split apart the string on, and it returns an array of strings
when you know your string uses a certain character to separate data, or if you want to operate on specific substrings individually.
returns a string consisting of the elements of the object repeated the given number of times.
when you want to repeat a string
retrieves the matches when matching a string against a regular expression
when you want to return an array of matching strings
returns the string character at a given index.
When you want to find a character at a given index
returns the unicode of the character at a specified index in a string. This an UTF-16 cone integer between 0 and 65535.
when you need to find the unicode character of a specific index in a string