JavaScript entry String Object "Novice must see" _javascript tips

Source: Internet
Author: User
Tags trim

One, String object

A string object is used to process text (strings).

Second, the constructor

New String (value)//constructor
function String (value)//Conversion functions

Third, the property

Length The number of characters in the string

      var str = new String ("ABCDEFG");
      document.write (str.length);   Output 7

Iv. methods

1. Chatat () takes the character of a specified position in a string.

      var str = new String ("ABCDEFG");
      document.write (Str.charat (1));   Output b

2, Chatcodeat () returns the encoding of the character at the specified position in a string.

      var str = new String ("ABCDEFG");
      document.write (Str.charcodeat (1));   Output 98

3. Concat () joins one or more values into a string.

      var str = new String ("ABCDEFG");
      var str1 = "Hijk";
      document.write (Str.concat (str1));   Output Abcdefghijk

4. INDEXOF () looks for the position of a character or string in the specified string. If not found return-1

Syntax: indexOf (str) str: SUBSTRING or character

IndexOf (str,start) str: substring or character. Start: Specify where the search starts

      var str = new String ("ABCCBA");
      document.write (Str.indexof (' B '));   Output 1
document.write (str.lastindexof ("BC"));   Output 1

This method realizes the contains effect and determines whether a string contains another string:

<script type= "Text/javascript" >
    window.onload = function () {
      var str1 = "Liu Bei";
      var str2 = "Liu Bei is a cattle man!";
      Alert (Str2.indexof (STR1)); Output 0 appears in position
      if (Str2.indexof (STR1) >-1) {
        alert ("Contains!");
      }
      else {
        alert ("Does not contain!")
      ;
    }
  </script>

5. LastIndexOf () looks for the position of a character or string in the specified string backwards (in reverse). If not found return-1

Syntax: LastIndexOf (str) str: SUBSTRING or character

LastIndexOf (str,start) str: substring or character. Start: Specify where the search starts

      var str = new String ("ABCCBA");
      document.write (Str.lastindexof (' B '));   Output 4

6. Localecompare () compares strings using locally defined order.

      var str = "ABCCBA";
      document.write (str.localecompare ("BC")); Output-1

7. Match () performs pattern matching using regular expressions.

8. Replace () performs a find-and-replace operation using a regular expression.

      var str = "ABCCBA";
      document.write (Str.replace ("B", "-")); Output A-CCBA

9. Search () finds strings in a string that match a regular expression.

      var str = "ABCCBA";
      document.write (Str.search ("B"));  Output 1

10, Slice () returns a slice or string of strings. If the argument is a negative number, it is from the back forward. Does not change the original string.

      var str = "ABCDEFG";
      document.write (Str.slice (2) + "<br/>"); Output CDEFG
      document.write (str);          The output ABCDEFG can see the original string and has not changed.

11. Split () is broken with the specified delimiter string or regular expression, and returns an array of strings.

      var str = "ABCDEFG";
      var arr = str.split ("D");
      document.write (Arr.join ());          Output ABC,EFG

12, substr () extracts a substring of a string, a variant of substring (). has been deprecated.

13, substring () extracts a substring of the string.

Syntax: substring (start,end) starts at start, ends with end, includes start but does not include ending. Does not change the original string.

      var str = "12345678";
      document.write (str.substring (1,4)); Output 234

14, toLowerCase () returns a lowercase copy of the specified string.

      var str = "AbcDEF";
      document.write (Str.tolocalelowercase ());  Output abcdef

15, ToString () returns the original string value.

      var str = "AbcDEF";
      document.write (Str.tostring ());  Output AbcDEF

16, toUpperCase () returns an uppercase copy of the specified string.

      var str = "AbcDEF";
      document.write (Str.touppercase ());  Output ABCDEF

17. Trim () Returns a copy of the blank before and after the specified string.

      var str = "  abcDEF  ";
      document.write ("one" + str.trim () + "one" + "<br/>");  Output 11abcdef11
      document.write ("one" + str + "one");  Output One AbcDEF 11

18, valueof () returns the original string value.

      var str = "AbcDEF";
      document.write (Str.valueof ());  Output AbcDEF

The above is the entire content of this article, I hope to help you, thank you for the support of cloud habitat community!

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.