JavaScript String Object
The String object is used to process existing blocks of characters.
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 use the position (index) to access any character in the string:
Example
var character=carname[7];
the index of the string is zero-based, so the first character of the string is [0], the second character is [1], and so on.
you can use quotation marks in strings, as follows:
Example
var answer="It's alright"; var answer="He is called 'Johnny'"; var answer='He is called "Johnny"';
or you can use the escape character (\) in a string using quotation marks:
Example
var answer='It\'s alright'; var answer="He is called \"Johnny\"";
String
String uses the length property length to calculate the length of the string:
Example
var txt="Hello World!"; document.write(txt.length); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write(txt.length);
Find the string in strings
Strings use indexOf() to locate the first occurrence of a specified character in the string:
Example
var str="Hello world, welcome to the universe."; var n=str.indexOf("welcome");
if no corresponding character function is found, -1 is returned
The lastIndexOf() method starts at the end of the string to find where the string appears.
Content matching
The match() function is used to find a specific character in a string and, if found, return that character.
Example
var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("world!"));
Replace the content
The replace() method replaces some characters with others in the string.
Example
str="Please visit Microsoft!" var n=str.replace("Microsoft","TutorialFish");
String case conversion
String case conversion uses the functions toUpperCase() / toLowerCase():
Example
var txt="Hello World!"; // String var txt1=txt.toUpperCase(); // txt1 text will be converted to uppercase var txt2=txt.toLowerCase(); // txt2 text will be converted to lower case
The string is converted to an array
strings are converted to arrays using the split() function:
Example
txt="a,b,c,d,e" // String txt.split(","); // Use comma to separate txt.split(" "); // Use spaces to separate txt.split("|"); // Use vertical bars to separate
Special characters
In Javascript, you can use backslash (\) to insert special symbols, such as apostrophes, quotation marks, and other special symbols.
Check out the JavaScript code below:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
In JavaScript, strings start and stop using single or double quotes. This means that the above string will be cut into: We are the so-called
to solve the above problem, you can use backslash to escape the quotation marks:
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 following table lists the other special characters that can be escaped using backslashes:
Code | Output |
---|---|
\' | apostrophe |
\" | double quotation marks |
\\ | diagonal bar |
\n | line breaks |
\r | enter |
\t | tab |
\b | space |
\f | page feeds |
String properties and methods
attribute:
length
prototype
constructor
method:
charAt()
charCodeAt()
concat()
fromCharCode()
indexOf()
lastIndexOf()
match()
replace()
search()
slice()
split()
substr()
substring()
toLowerCase()
toUpperCase()
valueOf()
For more methods and properties, see JavaScript String Objects