A string of JavaScript

Source: Internet
Author: User

first, the constructor function

New String (value)//constructor

function String (value)//Conversion functions

Second, the attribute

Length The number of characters in the string

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

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

            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.

            New String ("ABCDEFG");            document.write (Str.charcodeat (1));     // output 98  

3, concat () concatenate one or more values into a string.

            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 to start the search

            New String ("ABCCBA");            document.write (Str.indexof (' B '));     // Output 1  document.write (str.lastindexof ("BC"));     // Output 1    

Use this method to implement the contains effect to determine whether a string contains another string:

    <script type= "Text/javascript" >        function () {            var str1 = "Liu Bei";            var str2 = "Liu Bei is a bull man!" //if (Str2.indexof (STR1) >-1) {alert ("Included!") else {alert ("not included!") ); }} </script>          

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

Syntax: lastIndexOf (str) str: SUBSTRING or character

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

            New String ("ABCCBA");            document.write (Str.lastindexof (' B '));     // output 4  

6. Localecompare () compares strings using a 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 characters. If the argument is negative, the number from the back forward is indicated. Does not change the original string.

            var str = "ABCDEFG";            // output CDEFG            document.write (str);                    // output ABCDEFG You can see that the original string has not changed. 

11, split () breaks 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 (). is deprecated.

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

Syntax: substring (start,end) starts from start and ends with end, including start but not end. 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 specified string that is blank before and after the removal.

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

18, ValueOf () returns the original string value.

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

A string of JavaScript

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.