JavaScript String Methods


1. charAt (index): Returns the character at the specified index in a string.

Syntax:

    stringName.charAt(index);


Example:

    var website = “TutorialsDuniya”;
	document.write(website.charAt(4)); // prints r


2. concat(str1, str2): Concatenates two or more strings and returns the new concatenated string.

Syntax:

    string.concat(string1, string2,...,stringN);


Example:

    var str1 = "Tutorials";
    var str2 = "Duniya";
    var str3 = ".com";
    var website = str1.concat(str2, str3);
    document.write(website);   


3. endsWith(str): Returns true if the string ends with the specified string/characters else returns false.

Syntax:

    string.endsWith(“ending string”);


Example:

    var website = "TutorialsDuniya.com";
    document.write(website.endsWith(".com"));



4. includes(str): Returns true if the string includes the specified string/characters else returns false. This method is case sensitive.

Syntax:

    string.includes(“searchValue”);


Example:

    var website = “Tutorials Duniya”;
	document.write(website.includes(“Duniya”));


5. indexOf(str): Returns the index of first occurrence of the specified string. This method returns -1 if specified string is not found. This method is case sensitive.

Syntax:

    string.indexOf(str);


Example:

    var website = "TutorialsDuniya is the good source for the notes of CS subjects";
    document.write(website.indexOf("the")); // returns 19    


6. lastIndexOf(str): Returns the index of last occurrence of the specified string. This method returns -1 if specified string is not found. This method is case sensitive.

Syntax:

    string.lastIndexOf(str);


Example:

    var website = "TutorialsDuniya is the good source for the notes of CS subjects";
    document.write(website.lastIndexOf("the")); // returns 39    


7. match(regexp): Returns all the matches against a regular expression. If no match is found then it returns NULL.

Syntax:

    string.match(regexp);


Example:

    var website = “Tutorials Duniya”;
	document.write(website.match(/uni/); //prints uni



8. repeat(num): Returns a new string with specified number of copies of the string.

Syntax:

    string.repeat(num);


Example:

    var website = “TutorialsDuniya “;
	document.write(website.repeat(2)); // prints TutorialsDuniya TutorialsDuniya


9. replace(str1, str2): Returns a new string by replacing the specified string.

Syntax:

    string.replace(searchString, newString);


Example:

    var website = “Tutorials Duniya”;
	Document.write(website.replace(“Tutorials”, “Apni”)); //prints Apni Duniya


10. search(str): Returns the position of the first occurrence of the specified string and if no match is found, it returns -1.

Syntax:

    string.search(str);


Example:

    var website = “TutorialsDuniya”;
	document.write(website.search(“rial”)); // returns 4


11. slice(start, end): Returns a new extracted string from the original string.

Syntax:

    string.slice(start, end);


Example:

    var website = “TutorialsDuniya”;
	document.write(website.slice(2,8)); // prints torial



12. split(separator, limit): Returns a new array after splitting the original string into array of substrings.

Syntax:

    string.split(separator,limit);


Example:

    var website = "I am admin of Tutorials Duniya";
    document.write(website.split(" ",5)); // prints I,am,admin,of,Tutorials    


13. startsWith(str): Returns true if the string starts with the specified string/characters else returns false.

Syntax:

    string.startsWith(“starting string”);


Example:

    var website = "TutorialsDuniya.com";
    document.write(website.startsWith("Tutorials"));    


14. substring(start, end): Returns a new string with extracted characters from the original string between start and end (excluding end).

Syntax:

    string.substring(start, end);


Example:

    var website = “TutorialsDuniya”;
	document.write(website.substring(2,8)); // prints torial


15. toLowerCase(): Returns a new string from original string with all lowercase letters.

Syntax:

    string.toLowerCase();


Example:

    var website = “TutorialsDuniya”;
	document.write(website.toLowerCase()); // prints tutorialsduniya


16. toUpperCase(): Returns a new string from original string with all uppercase letters.

Syntax:

    string.toUpperCase();


Example:

    var website = “TutorialsDuniya”;
	document.write(website.toUpperCase()); // prints TUTORIALSDUNIYA



17. toString(): Returns the value of string object.

Syntax:

    string.toString();


Example:

    var website = “TutorialsDuniya”;
	document.write(website.toString()); prints TutorialsDuniya


18. trim(): Returns the same string after removing whitespaces from both sides.

Syntax:

    string.trim();


Example:

    var website = “    TutorialsDuniya    ”;
	document.write(website.trim()); prints TutorialsDuniya