String object for getting started with javascript [required for beginners]

Source: Internet
Author: User
String object for getting started with javascript [required for beginners] 1. String object

String object is used to process text (String ).

Ii. Constructor

New String (value) // Constructor
Function String (value) // Conversion function

Iii. Attributes

Length: the number of characters in the string.

Var str = new String ("abcdefg"); document. write (str. length); // output 7

Iv. Methods

1. chatAt () is used to retrieve characters at the specified position in a string.

Var str = new String ("abcdefg"); document. write (str. charAt (1); // output B

2. chatCodeAt () returns the encoding of characters at the specified position in a string.

Var str = new String ("abcdefg"); document. write (str. charCodeAt (1); // output 98

3. concat () concatenates one or more values into one string.

Var str = new String ("abcdefg"); var str1 = "hijk"; document. write (str. concat (str1); // output abcdefghijk

4. indexOf () searches for a character or string position in the specified string. If no result is found,-1 is returned.

Syntax: indexOf (str) str: substring or character

IndexOf (str, start) str: a substring or character. Start: Specifies the start position of the search.

Var str = new String ("abccba"); document. write (str. indexOf ('B'); // output 1document. write (str. lastIndexOf ("bc"); // output 1

Use this method to implement the Contains effect and determine whether a string Contains another string:

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.