Header Ads Widget

Responsive Advertisement

Most common string methods in javascript

            Basic string methods in javascript
  

1: Length

when we have to find the length of a string data:

const str="hello techietart"
console.log(str.length)
//We got an answer of 16

2:Trim

At the time of removing whitespace from the beginning and at the end

const str= "    hello techietart    "
console.log(str.length)      //24

console.log(str.trim().length)   // 16

3: Split

When we have to convert the string to an array or split the str or array from a specific symbol.

const str = " hello techies "let's do coding "
const newArr= str.split(" ")     // this line checks the str and splits the string when it finds space and makes an array.
console.log(newArr)     // ["hello", "techies", "let's", "do", "coding" ]

4: includes:

It returns true if it finds the particular string in the given string, else returns false.

 const str=" Hello techies, let's do coding"
if(str.includes("techies")){
console.log(true)
    } else{
        console.log(false)
       }
/// output will be true

5:chat at

To fetch the character at the specified position in a string:
 
const str=" Hello techies, let's do coding" 
const strAt=str.chatAt(1)
console.log(strAt)       // e

6: slice

Extract a part from a particular string 

const str=" Hello techies, let's do coding" 
const temp= str.slice(0,4)
console.log(temp)       //"Hell"

from parameter will be omitted in the output

 

7: To Lower Case 

convert string to lowercase
const str="HELLO TECHIE"
console.log(str.tolowercase())

output: hello techie

8: to Upper case

convert the string to upper case letters.
const  str=" hello tech world"
console.log(str.toUpperCase())
// output will be "HELLO TECH WORLD"

9:Replace 

return a new string which is replaced by a different string
const str="hello techie"
const temp=str.replace("hello", "best")

console.log(temp)  //best techie

10: Concat 

addition of two strings into a single one
const str="hello"
const str2="techie" 
const tempSolution= str.concat(" ",str2)
console.log(tempSolution)

//   "hello techie"


Post a Comment

0 Comments