This blog covers all important string methods which every javascript developer should master today. All string methods are explained with help of an example. These all string methods are very useful, if you are learning web development.
So, let's get started,
๐ท CHARAT()
The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
Example :-
var str = "HELLO WORLD";
var res = str.charAt(0);
console.log(res);
// output H
๐ท CONCAT()
The concat() method is used to join two or more strings. This method does not change the existing strings, but returns a new string containing the text of the joined strings.
Example :-
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2);
console.log(res);
// output Hello world!
๐ท CHARCODEAT()
The charCodeAt() method returns the unicode of the character at the specified index in a string.
Example :-
var str = "HELLO WORLD";
var res = str.charCodeAt(0);
console.log(res);
// output 72
๐ท ENDSWITH()
The endsWith() method determines whether a string ends with the characters of a specified string. This method returns true if the string ends with the characters, and false if not.
Example :-
var str = "Hello world, welcome to the universe.";
var res = str.endsWith("universe.");
console.log(res);
// output true
๐ท FROMCHARCODE()
The fromCharCode() method converts unicode values into characters. This is a static method of the String object, and the syntax is always String.fromCharCode()
Example :-
var res = String.fromCharCode(65);
console.log(res);
// output 4
๐ท INDEXOF()
The indexOf() method returns the position of the first occurrence of a specified value in string. This method returns -1 if the value to search for never occurs.
Example :-
var str = "Hello world, welcome to the universe.";
var res = str.indexOf("welcome");
console.log(res);
// output 13
๐ท LOCALECOMPARE()
The localeCompare() method compares two strings in the current locale. The locale is based on the language settings of the browser. Returns -1 if str1 is sorted before str2, returns 0 if the two strings are equal and returns 1 if str1 is sorted after str2.
Example :-
var str1 = "ab";
var str2 = "cd";
var res = str1.localeCompare(str2);
console.log(res);
// output -1
๐ท INCLUDES()
The includes() method determines whether a string contains the characters of a specified string.
Example :-
var str1 = "Hello world, welcome to the universe.";
var res = str.includes("world");
console.log(res);
// output true
๐ท LASTINDEXOF()
The lastIndexOf() method returns the position of the last occurrence of a specified value in a string. The string is searched from the end to the beginning, but returns the index starting at the beginning, at position 0.
Example :-
var str = "Hello planet earth, you are a great planet.";
var res = str.lastIndexOf("planet");
console.log(res);
// output 36
๐ท MATCH()
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
Example :-
var str = "The rain in SPAIN stays mainly in the plain";
var res = str.match(/ain/g);
console.log(res);
// output ["ain", "ain", "ain"]
๐ท REPEAT()
The repeat() method returns a new string with a specified number of copies of the string it was called on.
Example :-
var str = "Hello world!";
str.repeat(2);
console.log(str);
// output Hello world!Hello world!
๐ท SEARCH()
The search() method searches a string for a specified value, and returns the position of the match.
Example :-
var str = "Visit W3Schools!";
var res = str.search("W3Schools");
console.log(res);
// output 6
๐ท SPLIT()
The split() method is used to split a string into an array of substrings, and returns the new array.
Example :-
var str = "How are you doing today?";
var res = str.split(" ");
console.log(res);
// output ["How", "are", "you", "doing", "today?"]
๐ท REPLACE()
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
Example :-
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
console.log(res);
// output Visit W3Schools!
๐ท SPLICE()
The slice() method extracts parts of a string and returns the extracted parts in a new string.
Example :-
var str = "Hello world!";
var res = str.slice(0,5);
console.log(res);
// output Visit Hello
๐ท STARTSWITH()
The startsWith() method determines whether a string begins with the characters of a specified string.
Example :-
var str = "Hello world, welcome to the universe.";
var res = str.startsWith("Hello");
console.log(res);
// output true
๐ท SUBSTR()
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
Example :-
var str = "Hello world!";
var res = str.substr(1, 4);
console.log(res);
// output ello
๐ท TOLOCALELOWERCASE()
The toLocaleLowerCase() method converts a string to lowercase letters, according to the host's current locale.
Example :-
var str = "Hello world!";
var res = str.toLocaleLowerCase();
console.log(res);
// output hello world!
๐ท TOLOWERCASE()
The toLowerCase() method converts a string to lowercase letters. It does not change the original string.
Example :-
var str = "Hello world!";
var res = str.toLowerCase();
console.log(res);
// output hello world!
๐ท TRIM()
The trim() method removes whitespace from both sides of a string. It does not change the original string.
Example :-
let str = " Hello World ";
console.log(str);
// output: " Hello World "
let res = str.trim();
console.log(res);
// output: Hello World
๐ท SUBSTRING()
The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
Example :-
var str = "Hello world!";
var res = str.substring(1, 4);
console.log(res);
// output ell
๐ท TOLOCALEUPPERCASE()
The toLocaleUpperCase() method converts a string to uppercase letters, according to the host's current locale.
Example :-
var str = "Hello World!";
var res = str.toLocaleUpperCase();
console.log(res);
// output HELLO WORLD!
๐ท TOUPPERCASE()
The toUpperCase() method converts a string to uppercase letters. It does not change the original string.
Example :-
var str = "Hello World!";
var res = str.toUpperCase();
console.log(res);
// output HELLO WORLD!
Hope you liked this!
Let's Get Connected ๐ค:)