Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124


Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.
The length property is used to find the length of a string.
let str = 'phpforever'; console.log(str.length)
Output : 10
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
var str ="Find a string inside the string";
var pos = str.indexOf("string");
Output : 7
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
var str = "Find a string inside the string";
var pos = str.lastIndexOf("string");
Output:25
The search() method searches a string for a specified value and returns the position of the match.
var str = "Find a string inside the string";
var pos = str.search("string");
Output : 7.
There are 3 methods for extracting a part of a string.
The slice() Method.
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end not included).
Example.
var str = "Apple, Banana, Kiwi"; var res = str.slice(7, 13);
Output : Banana.
substring() is similar to slice(). The difference is that substring() cannot accept negative indexes.
var str = "Apple, Banana, Kiwi"; var res = str.substring(7, 13);
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
str = "Please visit phpforever!";
var n = str.replace("phpforever", "forever");
A string is converted to upper case with toUpperCase().
var text1 = "Hello World!"; var text2 = text1.toUpperCase();