Frequently Used JavaScript String Methods

Introduction

In this article, we are going to see about frequently used string methods in JavaScript.

JavaScript String Methods

fromCharCode()

Converts Unicode values to characters.    

String.fromCharCode(72, 101, 108, 108, 111);
// Output: Hello

charAt()

Returns the character at a specified index in a string.    

'Test'.charAt(2);
// Output: s

concat()

Concatenate the two or more strings 

'Hello'.concat(' ').concat('World')
// Output: Hello World

startsWith()

Returns true if the first character of the string is matched with a specified character, otherwise false. It is case-sensitive. 

'Test'.startsWith('T');
// Output: true

'Test'.startsWith('t');
// Output: false

'Test'.startsWith('e', 2);
// Here second parameter specifies the which position to start the search
// Output: false

'Test'.startsWith('e', 1);
// Output: true

endsWith()

Returns true if the end character of the string is matched with a specified character, otherwise false. It is case-sensitive. 

'Test'.endsWith('t');
// Output: true

'Test'.endsWith('T');
// Output: false

'Test'.endsWith('e', 2);
// Here second parameter specifies the length of the string to search
// Output: true

includes()

Returns true if the string contains the specified string, otherwise false.  

'Test'.includes('es');
//Output : true
JavaScript
indexOf     Returns the position (first occurrence) of the search value in the string     string.indexOf()    
'Test'.indexOf('s');
// Output: 2

match()

Search a string for a match against the regular expression. It returns null if no match is found.

'Test String'.match(/st/gi);
// Output: [st, St]

repeat()

Returns a new string with a specified number of copies of the specified string 

'T'.repeat(5);
// Output: TTTTT

replace()

Replace the search string with the specified string.

'Test String'.replace('String', 'Value');
// Output: Test Value

search()

Search the specified string value, and returns the position of the match, and returns -1 if no match is found.

'Test'.search('s');
// Output: 2

'Test'.search('a');
// Output: -1

slice()

Extracts the part of a string and returns it as a new string.

'Test'.slice(2, 4);
// Output: st

split()

Split the string into an array of substrings based on a split string.

'Test String Value'.split(" ");
// Output: ["Test", "String", "Value"]

toUpperCase()

It converts a string to uppercase letters.

'Test'.toUpperCase();
// Output: TEST

toLowerCase()

It converts a string to lowercase letters.

'Test'.toLowerCase();
// Output: test

trim()

Remove the whitespace from both sides of a string.

'  Test  '.trim();
// Output: Test

trimStart()

Remove the whitespace from the beginning of a string.

'  Test  '.trimStart();
// Output: 'Test  '

trimEnd()

Remove the whitespace from the end of a string.

'  Test  '.trimStart();
// Output: '  Test'

substr()

Extract the part of the string from the specified start position and the specified number of characters.

'Test'.substr(1, 2);
// Output: es

Hope you liked it and know about frequently used JavaScript string methods.