JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.
In this article, we’ll look at some useful string methods.
String
Various string methods are also very useful for manipulating strings.
string.indexOf(searchString, position)
The indexOf
method lets us search for the first occurrence of a substring.
We can choose to start from a given position
if we wish.
For instance, we can write:
console.log('foobar'.indexOf('bar', 4));
and get -1
because we started searching at index 4.
On the other hand, if we writeL
console.log('foobar'.indexOf('bar'));
We get 3 since bar
starts at index 3.
string.lastIndexOf(searchString, position)
lastIndexOf
searches for the index of the last occurrence of a given substring.
For instance, we can write:
console.log('foobarbar'.lastIndexOf('bar'));
and get 6 because the last 'bar'
starts in index 6.
string.localeCompare(that)
localeCompare
lets us compare 2 strings in a way that’s locale-specific.
For instance, we can write:
const m = ['FOO', 'foo'];
m.sort((a, b) => a.localeCompare(b));
Then we get that m
is [“foo”, “FOO”]
since the string order of 'foo'
is before 'FOO'
.
string.match(regexp)
We can use the match
method to get all the matches of a given regex in a string.
If the regex has the g
flag, then all instances of the match will be returned.
The capturing groups will be excluded.
For instance, we can write:
console.log('foobarbar'.match(/bar/g));
Then we get [“bar”, “bar”]
logged in the console.
string.replace(searchValue, replaceValue)
We can use the replace
method to replace a substring of a string with another substring.
A new string with the replaced substring is returned.
For instance, we can write:
const str = 'foo'
const bar = str.replace('foo', 'bar');
Then bar
is 'bar'
.
We can also use a regex instead of a string for searchValue
.
For instance, we can write:
const str = '555-1212'
const phone = str.replace(/d{3}/, '000');
console.log(phone);
Then we get ‘000–1212’
as the value of phone
. We searched for the part of a string with 3 digits and replaced it with ‘000’
.
The replace
method also can take a few string tokens that does various replacements as the value of replaceValue
.
For instance, '$$'
represents the dollar sign.
'$&'
represents the matched text.
$number
represents the capture group text.
$`
represents the text preceding the match.
$’
represents the text following the match.
For instance, if we have:
const str = '555-1212'
const phone = str.replace(/d{3}/, '$&-123');
Then phone
is ‘555–123–1212’
because we get the substring with 3 digits and added '-123'
after it.
string.search(regexp)
The search
method lets us search for the string with the given regex and return the index of the first occurrence that matches a given pattern.
For instance, we can write:
const str = '555-1212'
const index = str.search(/d{3}/);
Then we get 0 for index
since the first occurrence of a part of the string with 3 digits is at the start of the string.
string.slice(start,end)
slice
leys us make a new string by taking the part with the start
index to end
index minus 1 and return it.
For instance, we can write:
const str = '555-1212'
const phone = str.slice(0, 3);
and we get '555'
for phone
.
end
is optional and the indexes can be negative, in which case the count will start from the end of the string.
If we have:
const str = '555-1212'
const phone = str.slice(-3);
Then phone
is '212'
because we start from -3
, which is the 3rd last index of the string.
string.split(separator, limit)
The split
method splits a string by the separator
. limit
sets the maximum number of pieces that will be returned.
For instance, we can write:
const str = '5551212'
const arr = str.split('', 5);
Then we get [“5”, “5”, “5”, “1”, “2”]
as the value of arr
since we stop at 5 pieces.
string.substring(start,end)
substring
is the same as slice
but doesn’t work with negative indexes.
Conclusion
There are many useful JavaScript string methods in the standard library of JavaScript.
To make our lives easier, we should use them as much as possible.