JavaScript String Methods Explained with Examples
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'));







