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 endconst 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" ]
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
/// 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)
const strAt=str.chatAt(1)
console.log(strAt) // e
6: slice
Extract a part from a particular stringconst str=" Hello techies, let's do coding"
const temp= str.slice(0,4)
console.log(temp) //"Hell"
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
const str=" hello tech world"
console.log(str.toUpperCase())
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
const str="hello"
const str2="techie"
const str="hello techie"
const temp=str.replace("hello", "best")
console.log(temp) //best techie
10: Concat
addition of two strings into a single oneconst str="hello"
const str2="techie"
const tempSolution= str.concat(" ",str2)
console.log(tempSolution)
// "hello techie"
console.log(tempSolution)
// "hello techie"
0 Comments