JavaScript string processing (String object) Details _ basics

Source: Internet
Author: User

Defining a String Object

A JavaScript string object is used to work with text strings. The syntax for creating a String object is as follows:

Copy Code code as follows:

<script language= "JavaScript" >
var str_object = new String (str);
var str1 = String (str);
var str2 = str;
</script>



of the above three methods, only the first one is to use the string constructor to strictly define a string object and return an object. The second is to call the string function, and the conversion parameter str to the original string string and return. The third is to define a string variable, but the JavaScript is still processed by the string object.

You can tell the difference by running the following statement:

Copy Code code as follows:

Alert (typeof Str_object); Output Object
Alert (typeof str1); Output string
Alert (typeof str2); Output string

String Object Properties

Property Description
Constructor A reference to the function that created the object
Length Length of string
Prototype Adding properties 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 a Web page

String manipulation

Concat (): Connecting two or more strings
Replace (): string substitution or regular matching substitution
Slice (): to intercept a string by specifying a start and end position
Split (): Dividing a string into an array of strings
SUBSTR (): Intercepting strings based on start position and length
SUBSTRING (): To intercept a string by specifying a start and end position

String conversions

toLowerCase (): Converts a string to lowercase
toUpperCase (): Converts a string to uppercase
fromCharCode (): Converts one or more Unicode values to strings

String Lookup

CharAt (): Gets the character at the specified position
charCodeAt (): Gets the Unicode encoding of the specified location character
IndexOf (): Calculates the first occurrence of a specified string in a string
LastIndexOf (): Calculates the first occurrence of a specified string in a string

HTML Tag Class

Bold (): Gets the character at the specified position
FontColor (): Displays the string according to the specified color
FontSize (): Displays the string according to the specified size
Italics (): Display string as italic
Link (): Adds a hyperlink to a string
Strike (): Add Strikethrough to String
Sub (): Display string as subscript
SUP (): Display string as superscript

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.