Most Common Javascript String Methods You Need To Know:
JavaScript is one of the most popular languages nowadays.JavaScript is very much a high-level language. In programming, the string is a sequence of characters and also spaces and numbers. Javascript string has many built-in functions and it makes using javascript string easier.
.charAt() — This is used for finding the location for the character from a string. The charAt() method returns the character at the specified index in a string.
var str = programming;
var result = str.charAt(5)
console.log(result) //a
.concat() — this is used for adding two strings.
Using syntax is -
string.concat(string1, string2, …, stringX)
var str1 = “Hi “;
var str2 = “john!”;
var str3 = “whats up!”;
var result = str1.concat(str2, str3);
console.log(result) // Hi john! Whats up!
.includes() — A case sensitive search in string. It returns true or false.
var str = “What’s up man?”;
var result = str.includes(“up”)
console.log(result) // true
.endsWith() — searching a string last ends with the characters of a specified string. It also returns true or false.
var str = “Life is beautiful.”;
var result = str.endsWith(“beautiful.”);
console.log(result) // true
.indexOf() — Searching the position of string. If there are same value then returns the first position of the specified value in a string. This method returns -1 if the value to search for never found.
var str = “Hey broo! What are you doing here?.”;
var n = str.indexOf(“doing”);
console.log(result) // 23
.lastIndexof() — It mostly like .indexOf() method. The main difference is it returns the last position of the last occurrence of a specified value in a string. This method returns -1 if the value to search for never found.
var str = “Hello Rifath! Hello shifat!”;
var result = str.lastIndexOf(“Hello”);
console.log(result) // 14
.replace() — It searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. If their multiple matches the first one is replaced.
var str = “Never go there”;
var result = str.replace(“go”, “come”);
console.log(result) //Never come there
.toUpperCase() — It coverts a string to upper case letter.
var str = “Minhaz”;
var result = str.toUpperCase()
console.log(result) //MINHAZ
.toLowerCase() — This method converts a string lower case letter.
var str = “MINHAZ”;
var result = str.toLowerCase();
console.log(result) //minhaz
.trim() — This method removes white space from both sides of a string.
var str = “ I Love You “;
console.log(str.trim()); // “I Love You”
.trimStart() — This method removes whitespace from the beginning of a string.
var str = “ I Love You “;
console.log(str.trimStart()); // “I Love You ”
.trimEnd() — This method removes whitespace from the end of a string.
var str = “ I Love You “;
console.log(str.trimEnd()); // “ I Love You”