JavaScript String Methods - part 1

·

1 min read

.length

string.length

returns the length of a string

.slice()

string.slice(start, end)

returns the extracted part in a new string, end not included

If a parameter is negative, the position is counted from the end of the string.

If you omit the second parameter, the method will slice out the rest of the string.

.substring()

string.substring(start, end)

substring() is similar to slice().

The difference is that substring() cannot accept negative indexes.

.substr()

string.substr(start, length)

substr() is similar to slice().

The difference is that the second parameter specifies the length of the extracted part.

If you omit the second parameter, substr() will slice out the rest of the string.

If the first parameter is negative, the position counts from the end of the string.

.replace()

string.replace("old", "new")

The replace() method replaces a specified value with another value in a string.

The replace() method does not change the string it is called on.

The replace() method returns a new string.

The replace() method replaces only the first match

If you want to replace all matches, use a regular expression with the /g flag set.

To replace case insensitive, use a regular expression with an /i flag (insensitive).

excerpt from:https://www.w3schools.com/js/js_string_methods.asp