- String methods assist in using and modifying strings.
- You must change the string to an array before accessing it as an array using array methods.
Searches in a String
indexOf() is used to bring back an index of the first instance of a set character or text in the text string:
var loc = "Lets find where 'place' occurs!"; var plc = loc.indexOf("place");
lastIndexOf() is used to bring back an index of the last instance of a set character or text in the text string:
var plc = "Please locate where 'place' occurs!"; var loc = plc.lastIndexOf("place");
To find the starting index of a defined value,
search() is used:
var txt = "Let us find where 'place' starts!"; var loc = txt.search("place");
Note: search() and indexOf() may appear the same, but indexOf() does not accept regular expressions, while search() takes only one start position argument.
Extracting String Parts
The methods listed below are used for extracting a piece of a string:
var txt = "BMW, Audi, Mercedes"; var ans = txt.slice(7, 13);
When there is a negative value defined, the calculation is started from the end string location. In the code below, you can see a part of a string being sliced from -12th to -6th position:
var txt = "BMW, Audi, Mercedes"; var ans = txt.slice(-12, -6);
This method slices the other part of a string if the second parameter is excluded:
var ans = txt.slice(7);
It can be counted starting from the end:
var ans = txt.slice(-12);
Note: IE 8 does not support negative values.
var txt = "BMW, Audi, Mercedes"; var ans = txt.substring(7, 13);
Now, the substr() method has its own difference as well. The
length is defined in the second value parameter, thus, it can't be negative.=:
var txt = "BMW, Audi, Mercedes"; var ans = txt.substr(7, 6);
length property to bring it back. See the example below:
var lng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var x = lng.length;
There are also two methods that you can use when you need to take any characters of a string safely:
var txt = "HELLO WORLD"; txt.charAt(0); // returns H
charCodeAt() is used to return a unicode of a character at a defined index location in a string:
var txt = "HELLO WORLD"; txt.charCodeAt(0); // returns 72
Replacing String Content
replace() changes the defined value to another value:
txt = "I like to learn with bitdegree!"; var x = txt.replace("bitdegree", "bitdegree.org");
By default, the
replace() function replaces only the first match:
txt = "I like to learn with bitdegree. I like to learn with bitdegree."; var x = txt.replace("bitdegree", "bitdegree.org");
/g is used to switch out all matched up strings:
txt = "I like to learn. I like to learn with bitdegree."; var x = txt.replace(/bitdegree/g, "bitdegree.org");
replace() is case-sensitive so using capital letters will not make it work:
txt = "Lets visit bitdegree!"; var x = txt.replace("BITDEGREE", "bitdegree.org");
/i is used to solve this problem:
txt = "Let's visit bitdegree!"; var x = txt.replace(/bitdegree/i, "bitdegree.org");
Theory is great, but we recommend digging deeper!
toUpperCase() method converts the text into uppercase:
var txt1 = "Hi World!"; // String var txt2 = txt1.toUpperCase(); // txt2 is txt1 converted to upper
toLowerCase() method converts the text into lowercase:
var txt1 = "Hi World!"; // String var txt2 = txt1.toLowerCase(); // txt2 is txt1 converted to lower
var txt1 = "Hi"; var txt2 = "World"; var txt3 = txt1.concat(" ", txt2);
concat() is sometimes used as a plus operator. Both lines match in functionality:
var txt = "hi" + " " + "World!"; var txt = "hi".concat(" ", "World!");
Both of these methods will be returning a unique string. The primary string is not changed. That being said, strings can only be swapped out.
Converting a String to an Array
var str = "x,y,z,g,t"; // String str.split(","); // Split on commas str.split(" "); // Split on spaces str.split("|"); // Split on pipe
If we use an empty string as a separator (
""), the array is going to consist of single letter characters:
var str = "Hello"; // String str.split(""); // Split in characters
- There are various string methods you can use to manipulate strings.