JavaScript uses the Split function to intercept strings as specified (get mailbox username) _javascript Tips

Source: Internet
Author: User
A JavaScript basics technique, according to a string of characters specified in the string, to get the word specifier left or right string, in this case, the e-mail as an example, it can get the user name of the mailbox, and the user name outside the character discarded, this example should be more practical.
<ptml> <pead> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title>javascript intercept string </title> </pead> <body> <input id=" x "type=" Te XT "/> <input type=" button "onclick=" x () "Value=" Enter mail address, get user name "/> <script> function X () {var x=document.g Etelementbyid ("x"). value.tostring (); var c=x.split ("@"); document.getElementById ("x"). Value=c[0]; } </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

About the use of JS split is not much else to say what, the following direct examples for everyone to see
Copy Code code as follows:

<script language= "JavaScript" >
Str= "2,2,3,5,6,6"; This is a string
var strs= new Array (); Define an array

Strs=str.split (","); Character segmentation
for (i=0;i<strs.length; i++)
{
document.write (strs[i]+ "<br/>"); Character output after segmentation
}
</script>

The output result is
Copy Code code as follows:

2
2
3
5
6
6

JS Split is a string to a specific character into a number of strings, we should be a look to understand it.
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.