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.