String objects in JavaScript details _javascript tips

Source: Internet
Author: User
The sting string object is one of the built-in objects provided by JavaScript.

Note here that the first character in the string is the No. 0 digit and the second is the 1th digit.

1. How to create a string object

[Var] String Object Instance name = new String (string)

or var string object instance Name = String value

Example:

var str = "Hello world";

var str1 = new String ("This is a string");

Properties of 2.String

Length: Returns the lengths of the string

var intlength = Str.length//intlength = 11

Method of 3.String

CharAt (*): Returns a single character with a string at the first * bit

var x = "ABCDEFG"; var y = X.charat (3); y= "D"

charCodeAt (*): Returns the ASCII code of a single character with a string at the first * bit

Without repeating
Copy Code code as follows:

fromCharCode (): Accepts a specified Unicode value and returns a string.

document.write (String.fromCharCode (72,69,76,76,79)); The output is Hello

IndexOf (): Finds another string object from the string, finds the successful return position, or returns-1

document.write ("Children". IndexOf ("L", 0)); The output is 3.

document.write ("Children". IndexOf ("L", 1)); The output is 3.

document.write ("Children". IndexOf ("L", 4)); The output is 1.

LastIndexOf (): And the IndexOf () method is similar, the difference is to find the opposite direction, looking forward from the back

document.write ("Children". LastIndexOf ("L", 4)); The output is 3.

Split (separator character): Returns an array that is separated from the string, separating the characters that determine where the separation is.

' L&o&v&e '. Split (' & '); Returns an array l,o,v,e

SUBSTRING (): the equivalent of the cutting function of the string

SUBSTRING (< beginning >[,< final;])

document.write ("Children". Substring (1,3)); The output result is Hil

SUBSTR (): Also equivalent to cropping, note the difference with substring ()

SUBSTR (< beginning >[,< length)
Copy Code code as follows:

document.write ("Children". substr (1,3)); The output result is Hil. Here to notice compared with substing, although the results are the same, but the algorithm and ideas are different.

toLowerCase () and toUpperCase (): function similar, just return a string with the same original string, the only difference is that the former all letters are lowercase, the latter is uppercase.

document.write ("Love". toLowerCase ()); The output is love.

document.write ("Love". toUpperCase ()); The output is love.
Related Article

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.