Sitemap

JavaScript String Methods Explained with Examples

3 min readMar 8, 2023

--

Strings are everywhere in Javacript β€” Whether you are working with user input, API responses or formatting ouput. Thankfully javascript has a set of string methods to manipulate the text easily.

String is a datatype in programming languages which can be used to store text rather than numbers.A string sequence of characters can contain letters,numbers,spaces,symbols.It must be enclose within quotes to recognize it as a string.

In JavaScript we can create strings in two ways By using

β–ͺ️String Literal: let s1=”name”

β–ͺ️ String Object : let s2=new String(β€œhellooo”)

String Methods

The first thing we have to know about these string methods are ,They don’t modify the original string.

π’π’†π’π’ˆπ’•π’‰

length property returns the length of the string

let text="abcdefghijklmno"
//finding string length
let length=text.length
console.log(length)

π’”π’π’Šπ’„π’†( )

slice ( ) method extracts a part from the string and returns the extracted part as a new string

It takes two parameters :start position(included) and end position(excluded),if the end position is not mentioned then it will slice out the rest of the string.

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

let text="abcdefghijklmno"
//extracting a part of the string

//mentioned start and end
let partofText=text.slice(2,7)
console.log(partofText)

//mentioned only start
partofText=text.slice(6,)
console.log(partofText)

//negative parameter
partofText=text.slice(-9,-5)
console.log(partofText)

π’”π’–π’ƒπ’”π’•π’“π’Šπ’π’ˆ( )

It is similar to slice ( ),which can extracts a part of string

But The difference ,if the start and end values are given as negative they are treated as 0 and if we gave start is higher index and end as a lower index number,then the substring ( ) will return the string from the lower index till the higher index.

let text="abcdefghijklmno"
//extracting a part of the string

//mentioned start and end
let partofText=text.substring(2,7)
console.log(partofText)

//mentioned only start
partofText=text.substring(6,)
console.log(partofText)

//negative parameter
partofText=text.substring(-9,5)
console.log(partofText)

//mentioning start and end
partofText=text.substring(10,5)
console.log(partofText)

𝒔𝒖𝒃𝒔𝒕𝒓( )

substr ( ) is also similar to slice ( )

Get Anudeepthi kolagani’s stories inΒ yourΒ inbox

Join Medium for free to get updates fromΒ thisΒ writer.

But the difference the first is same as in slice( ) which mentions the start index but the second parameter specifies the length of the extracted part

let text="abcdefghijklmno"
//extracting a part of the string

//mentioned start and end
let partofText=text.substr(2,5)
console.log(partofText)

//mentioned only start
partofText=text.substring(4,)
console.log(partofText)

𝒓𝒆𝒑𝒍𝒂𝒄𝒆( )

replace ( ) method replaces a specified value with another value in string.By default the replace method will replaces only first match ,to replace all the matches we can use replaceAll( ) ,it was introduced in 2021.

let text="abcdefghijklmno"

//replacing a part of the string
let newText=text.replace("abcd","ABCD")
console.log(newText)

𝒄𝒐𝒏𝒄𝒂𝒕( )

conact ( ) can be used to join two or more strings,this can be used of plus operator ,to join the strings

let text1="abcdefg"
let text2="hijklmno"
//concatinating
let text3=text1.concat(" ",+text2)

𝒕𝒐𝑼𝒑𝒑𝒆𝒓π‘ͺ𝒂𝒔𝒆( ) 𝒂𝒏𝒅 π’•π’π‘³π’π’˜π’†π’“π‘ͺ𝒂𝒔𝒆( )

toUpperCase( ) used to convert the string into uppercase

toLowerCase( ) used to convert the string itno lowercase

let text1="abcdefg"
let text2="HIJKLMNO"
//converting Cases
let text1Upper=text1.toUpperCase();
let text2Lower=text2.toLowerCase();

console.log(text1Upper);
console.log(text2Lower);

𝒄𝒉𝒂𝒓𝑨𝒕( ), 𝒂𝒕 ( )

To know the character based on the given index in a string

charAt( ) takes positive numbers as parameters

at( ) can take negatives also and gives the character from the end of the list

let text1="abcdefg"

//finding the character at 2nd index
let char=text1.charAt(2)
let char=text1.charAt(-2)

let char=text1.at(2)
let char=text1.at(-2)

π’”π’‘π’π’Šπ’•( )

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

If the separator is omitted, the returned array will contain the whole string in index [0]. If the separator is β€œβ€, the returned array will be an array of single characters:

let text1="abcdefg,hijkl"
//splitting the string based on the delimiter
let s1=text1.split(",");
let s2=text1.split();
let s3=text1.split("");
console.log(s1)
console.log(s2)
console.log(s3)

π’Šπ’π’…π’†π’™π‘Άπ’‡( )

To know the index of character in a string

let text1="abcdefghijkl"
//finding index of b
console.log(text1.indexOf('b'));

--

--