JavaScript String Object
The String object is used to work with blocks of text.
JavaScript strings
A string is used to store a series of characters, like "John Doe".
A string can use single or double quotes:
Example
var carname="Volvo XC60";
var carname='Volvo XC60';
You can access any character in a string by its position (index):
Example
var character=carname[7];
String indexes start at zero, so the first character is [0], the second is [1], and so on.
You can use quotes inside a string, as in this example:
Example
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Or you can use escape characters () to use quotes inside a string:
Example
var answer='It's alright';
var answer="He is called "Johnny"";
Try it »
String
The String object uses the length propertylengthto calculate the length of a string:
Example
var txt="Hello World!";
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);
Try it »
Finding a string within a string
A string uses indexOf() to locate the first occurrence of a specified character or substring:
Example
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
Try it »
If no match is found, the function returns -1
The lastIndexOf() method searches from the end of a string for the position of a substring.
Matching content
The **match()** function is used to find specific characters in a string and returns them if found.
Example
var str="Hello world!";
document.write(str.match("world") + "
");
document.write(str.match("World") + "
");
document.write(str.match("world!"));
Try it »
Replacing content
replace() The method replaces some characters in a string with others.
Example
str="Please visit Microsoft!"
var n=str.replace("Microsoft","Runoob");
Try it »
String case conversion
String case conversion uses the functions toUpperCase() ** / toLowerCase()**:
Example
var txt="Hello World!"; // String
var txt1=txt.toUpperCase(); // txt1 text is converted to uppercase
var txt2=txt.toLowerCase(); // txt2 text is converted to lowercase
Try it »
Converting a string to an array
A string is converted to an array using the **split()** function:
Example
txt="a,b,c,d,e" // String
txt.split(","); // split by comma
txt.split(" "); // split by space
txt.split("|"); // split by vertical bar
Try it »
Special characters
In JavaScript, a backslash (\) can be used to insert special characters, such as apostrophes, quotes, and other special symbols.
Look at the following JavaScript code:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
In JavaScript, strings begin and end with single or double quotes. This means the string above will be cut into: We are the so-called
To solve the problem above, you can use a backslash to escape the quotes:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
JavaScript will output the correct text string: We are the so-called "Vikings" from the north.
The table below lists other special characters that can be escaped with a backslash:
| Code | Output |
|---|---|
| ' | Single quote |
| " | Double quote |
| \ | Slash |
| \n | Newline |
| \r | Carriage return |
| \t | tab |
| \b | Space |
| \f | Form feed |
String properties and methods
Properties:
- length
- prototype
- constructor
Methods:
- charAt()
- charCodeAt()
- concat()
- fromCharCode()
- indexOf()
- lastIndexOf()
- match()
- replace()
- search()
- slice()
- split()
- substr()
- substring()
- toLowerCase()
- toUpperCase()
- valueOf()
For more methods and properties, refer to:JavaScript String object