In this tutorial, you will learn about JavaScript String Methods with the help of examples. String methods in JavaScript are very helpful when you are going to work with JavaScript strings.
Headings of Contents
JavaScript String Methods
JavaScript String methods are the methods that only work with strings. When you are going to work with strings, Then you have various string methods to perform operations on them. JavaScript string methods make it easier to work with JavaScript string.
In this guide you will learn all the JavaScript string methods one by one using appropriate example.
String Length
To find the length of string you can use length property.
Example
var name = "programming Funda";
// Length of the string
console.log(name.length); // 16
Finding a string in a string
- To find the position of first occurrence of specified string from another string, use indexOf() method.
Example
var name = "programming Funda is programming portal";
console.log(name.indexOf('programming')); // 0
- To find the position of last occurrence of specified string from another string, use lastIndexOf() method.
Example
var name = "programming Funda is programming portal";
console.log(name.lastIndexOf('programming')); // 3
Both the method indexOf() and lastIndexOf() support the second parameter as the starting position for the search and also return -1 if the text is not found.
Searching For String in a String
To find the first occurrence of specified string from another string, use search() method.
Example
var name = "programming Funda is programming portal";
console.log(name.search('programming')); // 0
Note:- indexOf() and search() are equal.They accept the same parameter and return the same value. The only minor difference between both methods is indexOf() supports the second parameter but the search() does not support the second parameter.
Extracting Part of String
Some time we need to only some part of the string for which string provide three methods.
- slice()
- substring()
- substr()
The slice() Method
slice() is used to extract some part of the string and return extracted part as a new string. slice() support two parameters start position and end position, end position not included in the result.
Example
var name = 'Apple, Banana, Papaya, Grapes';
var result = name.slice(7, 13); // Banana
Note:- JavaScript count position from zero
0 is the first position in the string, 1 is the second position in the string,…
If the parameter is negative, the position is count from end.
Example
var x = 'Apple, Banana, Papaya, Grapes';
var result = x.slice(-14, -8);
If you omit the second parameter, the method will slice out the rest of the string.
Example
var name = 'Apple, Banana, Papaya, Grapes';
var result = name.slice(7);
console.log(result);
The output will be:- Banana, Papaya, Grapes
The substring() Method
substring() and slice() both are the same. The difference is that substring() not support negative index.
Example
var name = 'Apple, Banana, Papaya, Grapes';
var result = name.substring(7, 13);
console.log(result); // Banana
If you omit the second parameter, the substring() method will slice out the rest of the string.
The substr() Method
substr() is similar to the slice() method.
The difference is that the second parameter is the specified length of the extracted part.
Example
var name = 'Apple, Banana, Papaya, Grapes';
var result = name.substr(7, 6);
console.log(result); // Banana
If you omit the second parameter, The substr() method will slice the rest of the string.
Example
var name = 'Apple, Banana, Papaya, Grapes';
var result = name.substr(7);
console.log(result); // Banana, Papaya, Grapes
Replacing String Content
The replace() method replace a specified value with another value in a string.
Example
var name = 'Please visit google';
var res = name.replace("google", "Programming Funda");
console.log(res);
Output
Please visit Programming Funda
Note:– replace() doesn’t change the original string, return a new string. By default replace() method replace only the first matches.
By default replace() method is case sensitive. Below example will not work.
Example
var name = 'Please visit google';
var res = name.replace("GOOGLE", "www.programmingfunda.com");
To replace case-sensitive, use regular expression ( /i ) flag.
Example
var name = 'Please visit google';
var res = name.replace(/GOOGLE/i, "Programming Funda");
console.log(res);
Note:- regular expressions are written without quotes.
Output
Please visit Programming Funda
Important:- The replace() method does not change the string it is called on. It returns a new string.
To replace all the matches, use regular expression ( /g ) flag. g mean global.
Example
var name = 'Please visit google and google';
var res = name.replace(/google/g, "Programming Funda");
console.log(res);
Convert To Upper and Lower Case
There are two methods to convert upper and lower case.
- toLowerCase()
- toUpperCase()
- To convert upper case to lower case use toLowerCase() method.
var name = 'PROGRAMMINGFUNDA';
var result = name.toLowerCase();
console.log(result); // programmingfunda
- To convert lower case to upper case use toUpperCase() method.
var name = 'programmingfunda';
var result = name.toUpperCase();
console.log(result); // PROGRAMMINGFUNDA
The concat() Method
To join two or more string, use concat() method.
Example
var name1 = 'javascript';
var name2 = 'tutorial';
var result = name1.concat(' ', name2);
console.log(result);
The strim() Method
To remove whitespaces from both side of string, use strim() method.
Example
var name1 = ' javascript ';
var result = name1.strim();
console.log(result);
Extracting String Characters
The charAt() Method
The charAt() method returns the character at a specified index (position) in a string.
Example
var str = 'Hello world';
console.log(str.charAt(0)); // H
The charCodeAt() Method
The charCodeAt() method return the unicode of a character at a specific in a string.
Example
var str1 = 'Hello world';
console.log(str1.charCodeAt(0));
Converting a string to an Array
To convert a string into array, use split() method.
Example
var name = 'a,b,c,d';
var result = name.split(","); // split commas
var result = name.split(" "); // split space
var result = name.split("|"); // split pipe
Click here to learn all about JavaScript strings.
Conclusion
So, In this tutorial, we have learned all about JavaScript string methods with the help of examples.
I strongly recommend you, Before going through this article please read the JavaScript string tutorial, so that you don’t have any confusion regarding JavaScript string.
When you will be working with string in JavaScript, Then JavaScript string methods will very helpful to manage the string. You can not apply these methods to the other data type like numbers, arrays, etc.
If this article will have helped you, please share and keep visit for further JavaScript tutorials.