🍦String Methods
In JavaScript, strings are objects that have built-in methods that can be used to manipulate and analyze the string. Here are some of the most commonly used string methods:
length
: returns the number of characters in a string.
charAt(index)
: returns the character at a specified index in a string. The first character has an index of 0.
indexOf(searchValue, [start])
: returns the index of the first occurrence of a specified value in a string. If the value is not found, it returns -1. The optional start parameter specifies the position to start the search from.
lastIndexOf(searchValue, [start])
: returns the index of the last occurrence of a specified value in a string. If the value is not found, it returns -1. The optional start parameter specifies the position to start the search from, searching backwards.
slice(start, [end])
: returns a portion of a string as a new string. The start parameter specifies the index of the first character to include, and the optional end parameter specifies the index of the last character to include.
substring(start, [end])
: returns a portion of a string as a new string. It's similar to slice but it can't accept negative values as input.
substr(start, [length])
: returns a portion of a string as a new string. The start parameter specifies the index of the first character to include, and the optional length parameter specifies the number of characters to include.
replace(searchValue, replaceValue)
: replaces the first occurrence of a specified value with another value.
Last updated