String objects in Javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the String object in Javascript. If you need it, you can refer to the Sting String object, which is one of the built-in objects provided by Javascript.

Note that the first character in the string is 0th characters, and the second character is 1st characters.

1. method for creating 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 ");

2. String attributes

Length: returns the length of the string.

Var intlength = str. length // intlength = 11

3. String Method

CharAt (*): returns a single character whose character string is at the * bit.

Var x = "abcdefg"; var y = x. charAt (3); // y = "d"

CharCodeAt (*): returns the ASCII code of a single character whose character string is * characters.

Do not repeat

The Code is as follows:


FromCharCode (): receives a specified Unicode value and returns a string.

Document. write (String. fromCharCode (, 79); // The output result is HELLO.

IndexOf (): searches for another string object from the string, and returns the position after successful search. Otherwise,-1 is returned.

Document. write ("children". indexOf ("l", 0); // The output result is 3.

Document. write ("children". indexOf ("l", 1); // The output result is 3.

Document. write ("children". indexOf ("l", 4); // The output result is-1.

LastIndexOf (): similar to the indexOf () method, the difference is that the query direction is the opposite.

Document. write ("children". lastIndexOf ("l", 4); // The output result is 3.


Split (delimiter character): returns an array. The array is separated from the string, and the separator character determines the separation.

'L & o & v & e'. split ('&'); // returns the array l, o, v, e

Substring (): equivalent to the string cropping function.

Substring ( <始> [, <终> ])

Document. write ("children". substring (1, 3); // The output result is hil.

Substr (): it is also equivalent to cropping. Note that it is different from substring ().

Substr ( <始> [, <长度> ])

The Code is as follows:


Document. write ("children". substr (1, 3); // The output result is hil. In comparison with substing, though the results are the same, the algorithms and ideas are different.

ToLowerCase () and toUpperCase (): functions are similar, but returns a string with the same original string. The only difference is that all the letters of the former are lower-case and the latter is upper-case.

Document. write ("LOVE". toLowerCase (); // The output result is love.

Document. write ("love". toUpperCase (); // The output result is LOVE.

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.