There are a couple of javascript string search methods which is being asked in many interviews and you should know not because it is asked in interviews but also because it sorts some of your problems in codes.
Here is the list of String Search Methods:
1. IndexOf
inputString.indexOf(string) - This returns the first occurrence of a mentioned string in the given inputString.
inputString.indexOf(str, 10) - This returns the first occurrence of the string after the 10 positions.
var str = 'Hi, Welcome to the coding dev';
console.log(str.indexOf('Welcome'));
// 4
console.log(str.indexOf('Welcome', 10)); // Search after 10th position.
// -1
var str = 'Hi, Welcome to the coding dev - Welcome to JustCode';
console.log(str.indexOf('Welcome', 10)); // Search after 10th position.
// 32
console.log(str.indexOf('hi')); // case sensitive
// -1
2. lastIndexOf
Same as indexOf but searches from the end of the string.
inputString.lastIndexOf(string) - This returns the last occurrence of the mentioned string in the given inputString.
inputString.lastIndexOf(string, 10) - This returns the last occurrence of string from 10 index to the beginning.
var inputString = "Welcome to the coding dev";
console.log(inputString.lastIndexOf('to'));
//8
console.log(inputString.lastIndexOf('to', 14));
//8
console.log(inputString.lastIndexOf('the'));
//11
console.log(inputString.lastIndexOf('the', 6));
//-1
console.log(inputString.lastIndexOf('the', 15));
//11
console.log(inputString.lastIndexOf('The')); // case sensitive
//-1
3. search
The search() is the same as indexOf but won't allow position param.
Whereas, indexOf won't support regex based search but search() supports it.
var str = 'Hi, Welcome to the coding dev 2022';
console.log(str.search('Welcom'));
// 4
console.log(str.search(/\d/));
// 30
console.log(str.search(2022));
// 30
console.log(str.search('welcome')); // case sensitive
// -1
4. match
The match() searches specified string same like search but returns an array of matched strings.
var str = 'Hi, Welcome to the coding dev 2022';
console.log(str.match('Hi'));
// ['Hi', index: 0, input: 'Hi, Welcome to the coding dev 2022', groups: undefined]
console.log(str.match('Welcome'));
// ['Welcome', index: 4, input: 'Hi, Welcome to the coding dev 2022', groups: undefined]
console.log(str.match(/\d/));
// ['2', index: 30, input: 'Hi, Welcome to the coding dev 2022', groups: undefined]
console.log(str.match('hi')); // case sensitive
// null
5. includes
includes() returns true if string found.
var str = 'Hi, Welcome to the coding dev 2022';
console.log(str.includes(2022));
//true
console.log(str.includes('dev'));
//true
console.log(str.includes('this'));
//false
console.log(str.includes('hi')); // case sensitive
//false
6. startsWith
Returns boolean if string starts with specified value.
str.startsWith('test') - if 'str' starts with 'test' then returns true otherwise false.
str.startsWith('test', 10) - 10th index in string starts with test or not.
var str = 'Hi, Welcome to the coding dev 2022';
console.log(str.startsWith('Hi'));
// true
console.log(str.startsWith('Welcome', 4));
// true
console.log(str.startsWith('hi')); // case sensitive
// false
7. endsWith
Returns boolean if string ends with specified value.
str.endsWith('test') - if 'str' ends with 'test' then returns true otherwise false.
str.endsWith('test', 10) - till 10th index in string ends with test or not.
var str = 'Hi, Welcome to the coding dev';
str.endsWith('dev');
// true
str.endsWith('coding', 4); // Check first 4 characters ends with to.
// true
str.endsWith('dEv'); // case sensitive
// false
All the above methods are case sensitive methods.
I hope you will be going to use these functions, if you know some more function let us know in the comments.
0 Comments