JS Strings Method

·

6 min read

In this tutorial, we are going to know about different string methods

What is Strings?

A string is zero or more characters written inside quotes. We can use single or double quotes :

     var x = 'Hello World';   // Single quotes

     var y = "Javascript";    // Double quotes

We can use quotes inside a string, as long as they don't match the quotes surrounding the string:

var str   = "It's alright";
var str1 = "He is called 'Johnny'";
var str2 = 'He is called "Johnny"';

String Length

String length property returns the length of the string.

var str  = "QWERTYUIOPLKJHGFDSAZXCVBNM";
var res = str.length;   // returns 26

indexof()

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:

var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate"); // returns 7

lastIndexOf()

The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate"); // returns 21

Both indexOf(), and lastIndexOf() return -1 if the text is not found.

var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("John");   // returns -1

Both methods accept a second parameter as the starting position for the search:

var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate", 15);  // returns 21
var pos1 = str.lastIndexOf("locate", 15); // returns 7

The search() method searches a string for a specified value and returns the position of the match:

var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate"); // returns 7

Note:

indexof() and search() are not same. indexof() cannot takes regular expressions. search() cannot take second argument(starting position).

slice(start, end)

slice() extracts a part of a string and returns the extracted part in a new string.

The method takes 2 parameters: the start position, and the end position (end not included).

var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13); // returns Banana

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

var str = "Apple, Banana, Kiwi";
var res = str.slice(-12, -6); // returns Banana

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

var str = "Apple, Banana, Kiwi";
var res = str.slice(7); // returns Banana, Kiwi
var res1 = str.slice(-12); // returns Banana, Kiwi

substring(start, end)

substring() is similar to slice().

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

substr(start, length)

substr() is similar to slice().

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

replace()

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

By default, the replace() method replaces only the first match:

the replace() method is case sensitive.

str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "Apple"); // returns Please visit Apple and Microsoft!

toUpperCase()

A string is converted to upper case with toUpperCase():

var text1 = "Hello World!";       
var text2 = text1.toUpperCase(); // returns HELLO WORLD!

toLowerCase():

A string is converted to lower case with toLowerCase():

var text1 = "Hello World!";       
var text2 = text1.toLowerCase(); // returns hello world!

concat()

concat() joins two or more strings:

var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);  //  returns  Hello World!

trim()

The trim() method removes whitespace from both sides of a string:

var str = "       Hello World!        ";
alert(str.trim()); // returns Hello World!

padStart and padEnd

let str = "5";
str = str.padStart(4,0);  // result is 0005
str = str.padEnd(4,0);  // result is 5000

charAt()

The charAt() method returns the character at a specified index (position) in a string:

var str = "HELLO WORLD";
str.charAt(0);            // returns H

charCodeAt()

The charCodeAt() method returns the unicode of the character at a specified index in a string:

var str = "HELLO WORLD";

str.charCodeAt(0);         // returns 72

split()

A string can be converted to an array with the split() method:

Reference

MDN and W3School