Summary of using javascript string processing functions

Source: Internet
Author: User

1. Calculate the length of the string
CopyCode The Code is as follows: var TXT = "Hello world! "
Document. Write (txt. length)

2. indexof () method

How to Use indexof () to locate the first occurrence of a specified character in a string.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
VaR STR = "Hello world! "// W lower case
Document. Write (Str. indexof ("H") + "<br/>") // 0
Document. Write (Str. indexof ("world") + "<br/>") //-1
Document. Write (Str. indexof ("world") // 6
</SCRIPT>

3. Match () method

Find a specific character in the string, and if it is found, return this character.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
VaR STR = "Hello world! "
Document. Write (Str. Match ("world") + "<br/>") // world
Document. Write (Str. Match ("world") + "<br/>") // null
Document. Write (Str. Match ("worlld") + "<br/>") // null
Document. Write (Str. Match ("world! ") // World!
</SCRIPT>

4. Replace ()
Replace () is used to replace some characters in a string.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
VaR STR = "Visit cnblogs.com! "
Document. Write (Str. Replace (/cnblogs.com/, "mamogu.com "))
</SCRIPT>

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.