Latest web development tutorials

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure


DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript String (String) objects

String object is used to handle the existing character block.

JavaScript strings

A string is used to store a series of characters like "John Doe".

A string can use single or double quotes:


var carname = "Volvo XC60";
var carname = 'Volvo XC60';

You use the position (index) can access any of the string of characters:


var character=carname[7];

The zero-based index of the string, the first string of characters [0], the second character is [1], and so on.

You can use quotation marks in the string, the following examples:


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:


var answer='It's alright';
var answer="He is called "Johnny"";

try it"

String (String)

String (String) using the length property length to calculate the length of the string:


var txt="Hello World!";


try it"

Find a string in a string

Use string indexOf () to locate the position of a string in a specified character first appears:


var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");

try it"

If you do not find the corresponding character function returns -1

lastIndexOf () method to find the location at the end of the beginning of the string occurrences.

Content Match

match () function is used to find a particular string of characters, and if found, then return to this character.


var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");

try it"

Replace the contents

replace () method replaces some characters with some of the characters in a string.


str="Please visit Microsoft!"
var n=str.replace("Microsoft","w3cschool");

try it"

String case conversion

String case conversion using the function toUpperCase () / toLowerCase ():


var txt="Hello World!"; // String
var txt1=txt.toUpperCase(); // txt1 文本会转换为大写
var txt2=txt.toLowerCase(); // txt2 文本会转换为小写

try it"

String to an array

Use string split () function into an array:


txt="a,b,c,d,e" // String
txt.split(","); // 使用逗号分隔
txt.split(" "); // 使用空格分隔
txt.split("|"); // 使用竖线分隔

try it"

Special characters

Javascript can use the backslash (\) to insert special characters, such as: other special symbols apostrophes, quotes and so on.

See the following JavaScript code:

var txt="We are the so-called "Vikings" from the north.";

In JavaScript, strings start and stop the use of single or double quotes. This means that the string above will be cut: We are the so-called

Solve the above problems can use the backslash escape quotes:

var txt = "We are the so-called \" Vikings \ "from the north.";
document.write (txt);

JavaScript will output the proper text string: We are the so-called "Vikings" from the north.

The following table lists other special characters, you can use the backslash escape special characters:

Code Export
\ ' apostrophe
\ " Double quotes
\\ Diagonal rod
\ N Wrap
\ R Enter
\ T tab
\ B Blank

String properties and methods


  • length
  • prototype
  • constructor


  • charAt ()
  • charCodeAt ()
  • concat ()
  • fromCharCode ()
  • indexOf ()
  • lastIndexOf ()
  • match ()
  • replace ()
  • search ()
  • slice ()
  • split ()
  • substr ()
  • substring ()
  • toLowerCase ()
  • toUpperCase ()
  • valueOf ()