Detailed description of JavaScript String processing (String object), javascriptstring

Source: Internet
Author: User

Detailed description of JavaScript String processing (String object), javascriptstring

Define a String object

The JavaScript String object is used to process text strings. The syntax for creating a String object is as follows:
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Var str_object = new String (str );
Var str1 = String (str );
Var str2 = str;
</Script>

Among the above three methods, only the first one is to strictly define a String object using the String constructor, And the return is also an object ). The second method is to call the String function and convert the str parameter to the original String and return it. The third is to define a string variable, but it is still processed in JavaScript according to the string object.

Run the following statements to know their differences:
Copy codeThe Code is as follows:
Alert (typeof str_object); // output object
Alert (typeof str1); // output string
Alert (typeof str2); // output string

String object attributes

Attribute Description
Constructor Reference to the function that creates the object
Length String Length
Prototype Add attributes and methods to an object

String object Method

A list of common methods for String objects is as follows:

String output

Alert (): message warning box output text
Document. write (): output text to the Web page

String operation

Concat (): connects two or more strings
Replace (): String replacement or regular match replacement
Slice (): truncates a string by specifying the start and end positions.
Split (): Splits a string into a string array.
Substr (): truncates a string based on the start position and length.
Substring (): truncates a string by specifying the start and end positions.

String Conversion

ToLowerCase (): converts a string to lowercase.
ToUpperCase (): converts a string to uppercase.
FromCharCode (): converts one or more Unicode values to strings.

String search

CharAt (): gets the character at the specified position
CharCodeAt (): obtains the Unicode encoding of characters at the specified position.
IndexOf (): calculates the position where a specified string first appears in a string.
LastIndexOf (): calculates the position of a specified string that appears for the first time in the string.

HTML Tag class

Bold (): gets the characters at the specified position
Fontcolor (): the string is displayed in the specified color.
Fontsize (): displays the string according to the specified size.
Italics (): displays strings in italics.
Link (): adds a hyperlink to a string.
Strike (): adds strikethrough to a string.
Sub (): displays the string as a subscript.
Sup (): displays the string as a superscript.


What is the use of string objects in javascript?

Similar to the String object in JAVA, the String object in javascript also has its own method, which is similar to the String object in JAVA.

Var s = "abcd ";
S. substr (); // return "cd"
There are still many ways for the landlord to refer to this network
Www.w3school.com.cn/js/jsref_obj_string.asp

How does javascript solve the problem of converting strings into objects?

Javascript has a very useful function eval ("string ")
This function can convert a string to an object or execute a statement. For example, your code can use
Obj2 = eval ("document. a. c" + I), you can also write eval ("alert ()"),
Eval ("a + = 2"), so that you can run the javascript statement entered by the user.
Modify Your docheck () as follows:
Function docheck (){
Obj1 = eval ("document. a. a" + I)
Obj2 = eval ("document. a. c" + I)
Obj1.value = obj2.options [obj2.selectedIndex]. value;
}

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.