String Methods in Javascript

Hi Guys,

In this post we will learn about some JavaScript string methods with example.

Length()
Length function in js is used for returns the length of string.

Example:

let str = "Javascript length function"
console.log(str.length);

Slice Function
slice in js is used for extracts a part of string and return the extracted part, slice method takes two parameters like start and end position if the parameter is negative then the position is counted from the end.
Example:

let string = "Red, Green, Blue, Black"
string.slice(5,10)
string.slice(-3,-7)

Substring Function
Substring function in js is used to cut or extract the string and return the extracted part, it work similar to slice function but the difference in both is that end and start value less than zero are treated as zero in substring method.
Example:

let string = "Red, Green, Blue, Black"
string.substring(2,10)

Substr Function
substr functio is also worked like slice function but the defference in both is that the 2nd parameter specifies the length of extracted part.
Example:

let string = "Red, Green, Blue, Black"
str.substr(3,8)

Concate Function
concate in js is used for join two or more strings.
Example:

let string1 = "javascript"
let string2 = "example"
string1.concate(" ", string2)

Replace Function
Replace method in js is used to replace a specify value with another value in a string, it replace first match only and it is case sensitive method.

Example:

let string = "Use of replace function in javascript"
string.replace("function", "method")

ReplaceAll Function
ReplaceAll Method is used in js for replace a specified value in string instead of first match.

Example:

let string = "Use of replaceAll function in javascript, it is very important function"
string.replaceAll("function", "method")

toLowerCase function
toLowerCase Method in js is used for convert the characters into lowercase of string.

Example:

let string = "Use of toLowerCase function in javascript"
string.toLowerCase()

toUpperCase Function
toUpperCase Method in js is used for convert the characters into uppercase.

Example:

let string = "Use of toUpperCase function in javascript"
string.toUpperCase()

Split Function
Split Function in js is used for convert a string in to an array.

Example:

let string = "red,green,blue,black"
const arr = string.split(",")
console.log(arr[0])
console.log(arr[1])
console.log(arr[2])
console.log(arr[3])

charAt Function
charAt function is used in js for return the character at a specify index/position in a string

Example:

let string = "use of charAt function"
string.charAt(1)

trim function
Trim Function in js is used for remove the whitespace from both side of the string.
Example:

let string = "     use of trim function      "
string.trim()

trimStart function
trimStart function is used in js to remove the whitespace only from the start of string
Example:

let string = "     use of trimstart function      "
string.trimStart()

trimEnd function
trimEnd function in js is used to remove the whitespace only from the end of the string
Example:

let string = "     use of trimEnd function      "
string.trimEnd()

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *