Js-string String Object

Source: Internet
Author: User

Js-string String Object

A. String Object description
A string is a basic type of data for JavaScript.
The length property of the string object declares the number of characters in the string.
The string class defines a number of methods for manipulating strings, such as extracting characters or substrings from a string, or retrieving characters or substrings.
It is important to note that the JavaScript string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string.
In the earlier JavaScript implementations of the Netscape code base (for example, in Firefox implementations), the string behaves like a read-only character array. For example, extracting a third character from the string s, you can use s[2] instead of a more standard S.charat (2). Also, when you apply a for/in loop to a string, it enumerates an array subscript for each character in the string (but be aware that the length property cannot be enumerated by the ECMAScript standard). Because the array behavior of strings is not standard, you should avoid using it.

Second, String object properties

Properties Description
Constructor A reference to the function that created the object
Length The length of the string
Prototype Allows you to add properties and methods to an object

Three, String object methods

Method Description
Anchor () Creates an HTML anchor.
Big () Displays the string in a large font.
Blink () Displays the flashing string.
Bold () Use bold to display the string.
CharAt () Returns the character at the specified position.
charCodeAt () Returns the Unicode encoding of the character at the specified position.
Concat () The connection string.
Fixed () Displays the string as a typewriter text.
FontColor () Displays the string using the specified color.
FontSize () Displays the string using the specified dimensions.
fromCharCode () Creates a string from character encoding.
IndexOf () Retrieves a string.
Italics () Use italic to display strings.
LastIndexOf () The string is searched forward from the back.
Link () Displays the string as a link.
Localecompare () Compares two strings in a local-specific order.
Match () Finds a match for one or more regular expressions.
Replace () Replace the substring that matches the regular expression.
Search () Retrieves a value that matches a regular expression.
Slice () Extracts a fragment of a string and returns the extracted part in a new string.
Small () Use a small font size to display the string.
Split () Splits a string into an array of strings.
Strike () Use strikethrough to display strings.
Sub () Displays the string as subscript.
SUBSTR () Extracts a specified number of characters from the starting index number in a string.
SUBSTRING () Extracts the characters between two specified index numbers in a string.
SUP () Displays the string as superscript.
toLocaleLowerCase () Converts the string to lowercase.
toLocaleUpperCase () Converts the string to uppercase.
toLowerCase () Converts the string to lowercase.
toUpperCase () Converts the string to uppercase.
Tosource () Represents the source code for the object.
ToString () Returns a string.
ValueOf () Returns the original value of a string object.

Iv. detailed description of commonly used string objects

1, CharAt () returns the character at the specified position, and returns a string with a character length of 1


Stringobject.charat (Index)

The argument index must be a number that represents a position in the string, which is the subscript of the character in the string

Note: The subscript for the first character in the string is 0, and the last character's subscript is the string length minus 1

If the argument index is not between 0 and the string length minus one, the method returns an empty string

        <script>var mystr = "I love JavaScript";d Ocument.write (Mystr.charat (2));        </script>

2, IndexOf () returns the position of the first occurrence of a specified string value in a string

<script>var mystr = "I love JavaScript";d Ocument.write (Mystr.indexof ("I") + ' <br/> ');d ocument.write ( Mystr.indexof ("L") + ' <br/> ');d ocument.write (Mystr.indexof ("V"), 8);</script>

3. Split () string is split into an array of strings and returns this array

<script>var mystr = "www.baidu.com";//Use the specified symbol to split the string document.write (Mystr.split (".") + ' <br/> ');d ocument.write (Mystr.split (".", 2) + ' <br/> ');//Divide the string into characters document.write (Mystr.split ("") + ' <br/> ');d ocument.write (Mystr.split ("", 5);</script>

4, substring () extracts the character of a string intermediary between two specified subscripts

<script>var mystr = "I love JavaScript";d Ocument.write (mystr.substring (7) + ' <br/> ');d ocument.write ( Mystr.substring (2,6) + ' <br/> ');</script>

Js-string String Object

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.