JS Common API method

Source: Internet
Author: User

The commonly used Api:api of string objects refers to the application programming interface, which is actually some pre-programmed method.

The CharAt () method returns the character at the specified position. Stringobject.charat (Index)

The IndexOf () method returns the position of the first occurrence of a specified string value in a string. Stringobject.indexof (Searchvalue, Fromindex)

The substring () method is used to extract the character of a string intermediary between two specified subscripts. Stringobject.substring (Start, stop)

The slice () method extracts a portion of a string and returns the extracted part with a new string. Stringobject.slice (start, end)

The split () method is used to split a string into an array of strings. Stringobject.split (separator)

The replace () method is used to replace some characters in a string with some other characters stringobject.replace (REGEXP/SUBSTR, replacement)

            varStr= "Month white wind green all the Yi Xing Zhuang think fly, want to on the blue sky to embrace the moon"varNstr=str.charat (8) document.write ("The role of Charat" +nstr+ "<br>")                                varNstr1=str.indexof ("green");//the first occurrence of the character, from left to right to start the query. document.write ("The Role of IndexOf" +nstr1+ "<br>")                varNstr2=str.lastindexof ("green");//the first occurrence of the character, from right to left to start the query. LastIndexOf note the case of lettersdocument.write ("The Role of LastIndexOf" +nstr2+ "<br>")                varNstr3=str.substring (8)//Intercept from an element labeled eight, and intercept to the enddocument.write ("The Role of substring" +nstr3+ "<br>")                                varNstr4=str.substring (8,13)                //The element starting from subscript Eight is intercepted, and the element between 8 and 12 is truncated to the element labeled 13, including 8 and.document.write ("The Role of substring" +nstr4+ "<br>")                                varNstr4=str.substring ( -13,13)                //negative numbers cannot be taken, and negative numbers will be truncated from 0 to the element labeled 13. document.write ("The Role of substring" +nstr4+ "<br>")                                varNstr5=str.substr (4,7)//starting with the element subscript 4, the element containing the subscript 4 is truncated seven characters backward. That is, a string of length 7 is intercepted. document.write ("The Role of Substr" +nstr5+ "<br>")                                varNstr6=str.substr ( -7,7)//Seven characters are truncated from the bottom of the 7th element, including the bottom seventh element. That is, a string of length 7 is intercepted. document.write ("The Role of Substr" +nstr6+ "<br>")                                varNstr7=str.substr ( -9,-7)//a string of length 7 is intercepted starting from the bottom 9th element. However, the length cannot be negative and will be calculated as length 0, so it will not take effect. document.write ("substr role" +nstr7+ "does not take effect" + "<br>")                                varNstr8=str.slice (4,11)//The element with subscript 11 is not included until the element with subscript 4 is truncated to the element labeled 11. document.write ("The Role of Slice" +nstr8+ "<br>")                                varNstr9=str.slice ( -7,-5)//The bottom seventh element intercepts an element that follows the seventh element, and cannot be preceded by the seventh element. document.write ("The Role of Slice" +nstr9+ "<br>")                                varNstr10=str.split ("Green") document.write ("The role of Split" +nstr10+ "<br>")                                varNstr11=str.replace ("Qing", "Hua")//only the first green word found is replaced.document.write ("Function of Replace" +nstr11+ "<br>")

JS Common API method

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.