The best JavaScript String (String) objects Tutorial In 2024, In this tutorial you can learn JavaScript strings,Examples,Examples,Examples,Examples,String (String),Examples,Find a string in a string,Examples,Content Match,Examples,Replace the contents,Examples,String case conversion,Examples,String to an array,Examples,Special characters,String properties and methods,

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:

Examples

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

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

Examples

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:

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:

Examples

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



String (String)

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

Examples

var txt="Hello World!";
document.write(txt.length);

var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);


Find a string in a string

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

Examples

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

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.

Examples

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



Replace the contents

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

Examples

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



String case conversion

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

Examples

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



String to an array

Use string split () function into an array:

Examples

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



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.";
document.write(txt);

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
\ F PAGE


String properties and methods

Attributes:

  • length
  • prototype
  • constructor

method:

  • charAt ()
  • charCodeAt ()
  • concat ()
  • fromCharCode ()
  • indexOf ()
  • lastIndexOf ()
  • match ()
  • replace ()
  • search ()
  • slice ()
  • split ()
  • substr ()
  • substring ()
  • toLowerCase ()
  • toUpperCase ()
  • valueOf ()
JavaScript String (String) objects
10/30