10 Most Important String Methods

charAt(i) – this method returns the character at the provided index of i for a string. The index starts from left at 0.

var name = "Maria Doe";
name.charAt(1); //returns "a"

length – Method returns the length of the provided string.

var name = "Maria Doe";
name.length; //returns 9

split(delimiter,limit) -it returns an array containing each element according to the specified delimiter. limit is an optional parameter which will specify the length of a return array using split.

var name = "Welcome Maria Doe";
var name_arr = name.split(' '); //splits the string using space
console.log(name_arr); //output: ["Welcome", "Maria", "Doe"]

var first_elem = name.split(' ',1);
console.log(first_elem); //output: ["Welcome"]

concat(str1,str2,str3…) – returns a new string by merging the provided strings as parameter.

var welcome= "Welcome";
var first_name = " James";
var last_name = " Bond";

var new_str = welcome.concat(first_name,last_name) //returns "Welcome James Bond"

substr(start, length) – return the characters from the provided string but begins from the index provided as first parameter. length is an optional parameter but if provided it determines the length after the starting index has been provided.

var name = "Welcome Maria Doe";
var new_name = name.substr(8); //returns "Maria Doe"
var first_name = name.substr(8,5); //return "Maria"

slice(start, end) – return the sub string from the provided starting index. second parameter is an optional but if provided it is the ending index. slice can accept the negative indexes as well. When provided with the negative index, it starts counting from the end of the string.

var name = "Welcome Maria Doe";
var new_name = name.slice(8); //returns "Maria Doe"
var first_name = name.slice(8,13); //return "Maria"
var last_name = name.slice(-3); //return "Doe"

replace(regexexp | substr, newsubstr|function) – return a new string by searching the substring or regex into the provided string and replaces using newsubstr or function provided which returns the replacer.

var name = "Welcome Maria Doe";
var say_hi = name.replace("Welcome","Hello"); //returns "Hello Maria Doe"

function replacer(match){
  return match.toLowerCase();
var greetings = "HELLO world";
//use function as replacer
var welcome_lower = greetings.replace("HELLO",replacer); //returns "hello world"

indexOf(substr,start) – returns the index of the found sub string from the provided string. Second parameter is an optional parameter which determines from which length the sub string index search should start.

var name = "Welcome James Bond! Welcome";
var find_index = name.indexOf("Welcome"); //returns 0
var find_index1 = name.indexOf("Welcome",5); // returns 20 since the substring search started from 5th position into the string 

toLowerCase() – returns the string by converting all the characters to lower case.

var name = "Welcome James Bond";
var lower_name = name.toLowerCase(); //return "welcome james bond"

toUpperCase() – returns the string by converting all the characters to upper case.

var name = "Welcome James Bond";
var lower_name = name.toUpperCase(); //return "WELCOME JAMES BOND"

I hope this post was helpful to you. If you like my post, follow me @twitter/andramazo.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top