JavaScript StringObject
The String object is used to handle existing character blocks.
JavaScript Strings
A string is used to store a series of characters like "John Doe".
A string can use single or double quotes:
Examples
var carname='Volvo XC60';
You can access any character in a string using its position (index):
Examples
String indexes start from zero, so the first character of a string is
You can use quotes in a string, as in the example below:
Examples
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Or you can use escape characters (\) in a string to use quotes:
String
The String object uses the length propertylengthto calculate the length of a string:
Examples
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);
Try it yourself »
Find a String in a String
Strings use indexOf() to locate the first occurrence of a specified character in a string:
Examples
var n=str.indexOf("welcome");
Try it yourself »
Returns -1 if the corresponding character is not found.
The lastIndexOf() method starts searching from the end of the string for the position where the string appears.
Content Matching
match()The function is used to find a specific character in a string and returns that character if found.
Examples
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
Try it yourself »
Replace Content
replace()The method replaces some characters with other characters in a string.
String Case Conversion
Use functions for string case conversiontoUpperCase() / toLowerCase():
Examples
var txt1=txt.toUpperCase(); // txt1 text will be converted to uppercase
var txt2=txt.toLowerCase(); // txt2 text will be converted to lowercase
Try it yourself »
Convert String to Array
Strings use thesplit()function to convert to an array:
Examples
txt.split(","); // separated by commas
txt.split(" "); // separated by spaces
txt.split("|"); // separated by vertical bars
Try it yourself »
Special Characters
In JavaScript, you can use backslash (\) to insert special symbols, such as apostrophes, quotes, and other special symbols.
Look at the following JavaScript code:
document.write(txt);
In JavaScript, strings are started and stopped with single or double quotes. This means the above string will be cut into: We are the so-called
To solve the problem above, you can use a backslash to escape the quotes:
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 |
| \\ | Backslash |
| \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
Other Extensions