String object in JavaScript, javascriptstring

Source: Internet
Author: User

String object in JavaScript, javascriptstring

1. String: stores a String and provides the attributes and methods required to process the String.

1. Create a String object: Display and implicit

<DOCTYPE html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 

* Explicit and implicit declarative String objects are of different types: the string objects implicitly declared, whose type is string, and the string objects explicitly declared whose type is object.

* The real difference between explicit and implicit string creation is that if you want to reuse the same string, it is more efficient to create a string explicitly;

* Explicitly creating strings also helps JavaScript interpreters confuse numbers and strings;

2. Use the String object Method

There are many methods for String objects. Here we only discuss two. IndexOf () and substring () methods; case sensitive.

* You need to know that the JavaScript string is composed of characters. Each of these characters has an index. This index starts from 0, so the index at the first position is 0; the index at the second position is 1, and so on.

* The indexOf () method is used to find and return the starting index position of the substring. If the searched element does not exist,-1 is returned. Otherwise, the index of the character is returned. (LastIndexOf returns the position at which the substring ends)

The following is an example of determining whether the Email address entered by the number of users contains the @ symbol:

<DOCTYPE html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 

The substring () method uses the index of the start position and end position of the string as the parameter to extract a string from another string. You can return all strings from the first index to the end of the string without using the second parameter. For example, to intercept all characters from the third to the sixth character, you can write as follows:

<DOCTYPE html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 

The above is all the content of this article. I hope you will like it.

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.