DEV Community

Sowntharya
Sowntharya

Posted on

String methods

1.trim() :
This method removes whitespace from both sides of a string.

let text1 = "      hey hi!      ";
let text2 = text1.trim();   //hey hi!
Enter fullscreen mode Exit fullscreen mode

2.trimStart() :
This method removes whitespace only from start of a string.

let text1 = "      hey hi!      ";
let text2 = text1.trim();   //hey hi!
Enter fullscreen mode Exit fullscreen mode

3.trimEnd() :
This method removes whitespace only from end of a string.

let text1 = "      hey hi!      ";
let text2 = text1.trim();   //     hey hi!
Enter fullscreen mode Exit fullscreen mode

4.repeat() :
method returns a string along with a number of copies of a string.

let text = "python";
let result = text.repeat(2);   //pythonpython
Enter fullscreen mode Exit fullscreen mode

5.replace() :
method replaces a specified value with another value in a string.
note :

  • method does not change the string.
  • method replaces only the first match.
let text = "Please visit Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");  //Please visit W3Schools!
Enter fullscreen mode Exit fullscreen mode

the replace() method replaces only the first match.

let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");  //Please visit W3Schools and Microsoft!
Enter fullscreen mode Exit fullscreen mode

the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not work.

let text = "Please visit Microsoft!";
let newText = text.replace("MICROSOFT", "W3Schools"); //Please visit Microsoft!
Enter fullscreen mode Exit fullscreen mode

To replace case insensitive, use a regular expression with an /i flag.

let text = "Please visit Microsoft!";
let newText = text.replace(/MICROSOFT/i, "W3Schools");
//output : Please visit W3Schools!
Enter fullscreen mode Exit fullscreen mode

To replace all matches, use a regular expression with a /g flag.

let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "W3Schools");
output :  Please visit W3Schools and W3Schools!
Enter fullscreen mode Exit fullscreen mode

6.replaceAll() :
note : replaceAll() does not work in Internet Explorer.

let text = "I love cats. Cats are very easy to love. Cats are very popular."
text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");

 //output : I love dogs. Dogs are very easy to love. Dogs are very polpular.
Enter fullscreen mode Exit fullscreen mode

regular expression, the global flag (g) must be set.

text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");
Enter fullscreen mode Exit fullscreen mode

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

let text = "Hi fox!";
const myArr = text.split("");  //["H","i","f","o","x","!"]
Enter fullscreen mode Exit fullscreen mode
syntax :
text.split("")     // Split on characters
text.split(",")    // Split on commas
text.split(" ")    // Split on spaces
text.split("|")    // Split on pipe
Enter fullscreen mode Exit fullscreen mode

If the separator is omitted, the returned array will contain the whole string in index [0].

let text = "The quick brown fox.";
const myArr = text.split();  //["The quick brown fox."]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)