JavaScript strings (String) object

Source: Internet
Author: User

1.JavaScript Strings (String) object

A String object is used to handle an existing block of characters.

1.1 JavaScript string

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

A string can use either single or double quotation marks:

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

You use the location (index) to access any character in the string:

var character=carname[7];

The index of the string starts at zero, so the first character of the string is [0], the second character is [1], and so on.

You can use quotation marks in a string, as in the following 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 the string to quote:

var answer='it\ ' s alright'; var answer="He is called \" johnny\"";

1.2 Strings (String)

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

var txt="Hello world! " ;d Ocument.write (txt.length); var txt="abcdefghijklmnopqrstuvwxyz";d ocument.write (txt.length);

1.3 Finding a string in a string

The string uses indexOf () to position the first occurrence of a specified character in a string:

var str="Hello World, Welcome to the universe. " " ; var n=str.indexof ("welcome");

If the corresponding character function is not found, return-1

The LastIndexOf () method starts at the end of the string to find where the string appears.

1.4 Content Matching

The match () function is used to find a specific character in a string and, if found, returns the character.

var str="Hello world! " ;d ocument.write (Str.match ("World"<br>" );d Ocument.write (Str.match ("World"<br>  ");d ocument.write (Str.match ("world! "));

1.5 Replacing content

The Replace () method replaces some character with some characters in a string.

str="pleasevisit microsoft! " var n=str.replace ("Microsoft","runoob");

1.6 String Case Conversion

The string case conversion uses the function touppercase () / toLowerCase ():

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

1.7 String to Array

The string is converted to an array using the split () function:

txt="a,b,c,d,e"   //  Stringtxt.split ("," );   // use commas to separate txt.split ("");   // use spaces to separate txt.split ("| ");   //

1.8 Special Characters

In Javascript, you can use backslashes (\) to insert special symbols, such as apostrophes, quotation marks, and other special symbols.

Look at the following JavaScript code:

var txt="Vikings" from the North . " ;d ocument.write (TXT);

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

To resolve the above problems, you can use backslashes to escape quotation marks:

var txt="We is the so-called \" Vikings\ " from the north. " ;d ocument.write (TXT);

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

The following table lists other special characters that you can use to escape special characters by using backslashes:

Code Output
\‘ Single quotation marks
\" Double quotes
\\ Diagonal Bar
\ n Line break
\ r Enter
\ t tab
\b Space
\f Page change

1.9 String properties and methods

Property:

    • Length
    • Prototype
    • Constructor
    • Method:
      • CharAt ()
      • charCodeAt ()
      • Concat ()
      • fromCharCode ()
      • IndexOf ()
      • LastIndexOf ()
      • Match ()
      • Replace ()
      • Search ()
      • Slice ()
      • Split ()
      • SUBSTR ()
      • SUBSTRING ()
      • toLowerCase ()
      • toUpperCase ()
      • ValueOf ()

JavaScript strings (String) object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.