JavaScript String Methods
JavaScript String global object prototype provides predefined methods and properties to handle string related operations, making it easier for developers to perform text processing. In this tutorial, we will discuss some of the most useful String methods with code examples.
Javascript String concat(str)
Method
The syntax for this method is str1.concat(str2)
and its used to join the two strings str1
and str2
and returns the concatenated string as the result.
String concat()
Method Example:
Below we have a simple code example to see the concat()
function in action:
let a = "Welcome to Studytonight";
let b = " to learn JavaScript";
let c = a.concat(b);
console.log(c); // writes "Welcome to Studytonight to learn JavaScript"
This function just acts like the concatenation operator +
which is also used to join two strings.
JavaScript String toLowerCase()
Method
This method is used to convert a string into lowercase letters. It's a simple function, which operates on one string at a time and coverts all its alphabetical characters into lower case. Let's take an example,
let a = "WELCOME";
let b = a.toLowerCase();
console.log(b); // Logs: welcome
welcome
JavaScript toUpperCase()
Method
This method converts the string into uppercase letters and returns the result in the form of upper case. Let's take an example for this:
let x = "studytonight";
let y = x.toUpperCase();
console.log(y);
STUDYTONIGHT
JavaScript String search()
Method
This method searches a string or a part of the string(sub string) in a given string value and returns the position of the first occurrence of the sub string to be searched.
The syntax for using the search()
function is str1.search(searchStr);
where str1
is the string in which we want to search for searchStr
string.
let str = "Please locate where locate occurs!";
console.log(str.search("locate")); // Logs: 7
7
JavaScript String slice()
Method
JavaScript String slice()
method is used to extract a part of a string(a substring) and it returns the extracted part as a new string. This method takes two parameters, the starting index position and ending index position(where the ending index is not included in the output).
If the parameter is negative in that case position is counted from the end of the String.
Let's take an example to understand:
let a = "Orange Banana Mango";
let subA = a.slice(7,14); //result of subA will be Banana
console.log(subA)
Banana
In the example above, we have used the slice()
method to extract out the part of string starting from 7th index which is B character and upto the 14th index which means till the space after Banana.
Let's try with negative index now:
let a = "Orange Banana Mango";
let subA = a.slice(-7,19); //result of subA will be "a Mango"
console.log(subA)
a Mango
In the code example above, we have specified the first index as -7 so it will be counted from the end and the second index as positive so it will be counted from front. You can also use both negative values too.
JavaScript String split()
Method
It is used to split a string based on a given delimeter like space, hyphen etc and it returns an array with the broken parts of the string. Let's take an example to understand how it works:
let str = "x,y,z,u,v"; // this is a string
let arr = str.split(","); //split based on commas
console.log(arr[0]); // Logs: x
console.log(arr[1]); // Logs: y
x
y
JavaScript String charAt()
Method
This method returns the character at a specified index (position) in a string. It takes index value as an argument.
Let's take an example and see:
let str = "Hello Studytonight";
let s = str.charAt(0); //returns H
console.log(s);
H
JavaScript String trim()
Method
The trim()
method removes extra spaces from the starting and ending of the string. Let's take an example to understand how it works.
let str = " Hello StudyTonight JavaScript! ";
console.log(str.trim());
Hello StudyTonight JavaScript!
In the above example we have a string with extra blank spaces in the front and at the end. So we used the trim()
method to remove them. This method should be used to validate and modify user input in forms to remove unnecessary white spaces added by the user in any form value.
JavaScript String replace()
Method
JavaScript string replace()
method replaces a specified part of the string(sub string or character) with another value in the given string. By default, this method only replaces the first occurrence of the substring which means if the substring to be replaced is present two times in the main string then only the first occurence will be replaced and also this method is case-sensitive.
Let's take an example to understand how it works:
let txt = "please visit google";
console.log(txt.replace("google", "StudyTonight"));
let newTxt = "Try to Google 'Google'";
console.log(newTxt.replace("Google", "Bing"));
please visit StudyTonight
Try to Bing 'Google'
As you can see in the example above that only the first occurrence of substring "Google" is replaced while the second one is left as it is.
JavaScript String substring()
Method
This method is similar to the slice()
method.The only difference is that the substring()
method does not accept a negative index. In this method we can extract out a part of the main string by providing the starting and the ending index. Let's take an example:
let str = "C,JAVA,PYTHON";
let res = str.substring(2,6);
console.log(res) // Logs: JAVA
JAVA
JavaScript String substr()
Method
The substr()
method is similar to the slice()
method as it takes negative value too for index but there is one difference. The substr() method takes length of string to be extracted as the second parameter. For example, you can specify starting from index 2, pick the string of length 7, which means till the index 9.
Let's take an example to understand this:
let fruits = "Apple, Banana, Kiwi, Orange";
let substr = fruits.substr(6,6);
console.log(substr);
Banana
JavaScript String Built-In Methods:
Following are the built-in String global object methods which we can use with JavaScript strings.
Method |
Description |
charAt() |
It returns the character at the specified index (position) |
|
charCodeAt() |
It returns the Unicode of the character at the specified index |
|
codePointAt() |
The codePointAt() method returns a non-negative integer that is the Unicode code point value. |
concat() |
It joins two or more strings, and returns a new joined strings |
|
endsWith() |
It checks whether a string ends with specified string/characters |
|
includes() |
It checks whether a string contains the specified string/characters |
|
indexOf() |
It returns the position of the first found occurrence of a specified value in a string |
|
lastIndexOf() |
It returns the position of the last found occurrence of a specified value in a string |
|
localeCompare() |
It compares two strings in the current locale |
|
match() |
It searches a string for a match against a regular expression, and returns the matches |
|
repeat() |
It returns a new string with a specified number of copies of an existing string |
|
replace() |
It searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced |
search() |
It searches a string for a specified value, or regular expression, and returns the position of the match |
slice() |
It extracts a part of a string and returns a new string |
|
split() |
It splits a string into an array of substrings |
|
startsWith() |
It checks whether a string begins with specified characters |
|
substring() |
It extracts the characters from a string, beginning at a specified start position, and through the specified number of character |
toLocaleLowerCase() |
It converts a string to lowercase letters, according to the host's locale |
|
toLocaleUpperCase() |
It converts a string to uppercase letters, according to the host's locale |
|
toLowerCase() |
It converts a string to lowercase letters |
|
toString() |
It returns the value of a String object |
|
toUpperCase() |
It converts a string to uppercase letters |
|
trim() |
It removes whitespace from both ends of a string |
|
valueOf() |
It returns the primitive value of a String object |
|
In this tutorial we covered the important String methods which are most commonly required or used in development.