A string object in JavaScript

Source: Internet
Author: User

A. String: Stores a string and provides the properties and methods needed to handle the string.

1. Create a String object: Display and implicit

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <doctype html>

* Explicitly and implicitly declaring a string object, the type is different: an implicitly declared string object, the type is string, and the explicitly declared string object, the type is object.

* Explicit and implicit creation of strings the real difference is that if you want to reuse the same string, explicitly create the string, there is a higher efficiency;

* Create strings explicitly and also help the JavaScript interpreter to confuse numbers and strings;

2. Methods for using a String object

String object, there are many ways to discuss only two. IndexOf () and substring () method, note case.

* What you need to know: JavaScript strings, which are made up of characters. Each of these characters has an index. This index starts at 0, so the index for the first position is 0; the second is 1, and so on.

* Method IndexOf () finds and returns the index position at which the substring originated, returns 1 if the element found does not exist, or returns the index where the character is located. (LastIndexOf returns the position where the substring ends)

Here is an example of whether to include the @ symbol in an email address that determines the number of users entered:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 <doctype html>

The substring () method intercepts a string from another string using the index of the starting and ending positions of the string as arguments. You can return all the strings from the first index to the end of the string without using the second argument. For example, we want to intercept all characters from the third to the sixth character, which can be written like this:

1 2 3 4 5 6 7 8 9 <doctype html>

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.