JavaScript String Split method misoperation _javascript Tips

Source: Internet
Author: User
Split definition and usage
The split () method is used to split a string into an array of strings.
Grammar
Stringobject.split (separator, howmany) parameter description
Separator required. A string or regular expression that splits the Stringobject from the place specified by the parameter.
Howmany Optional. This parameter specifies the maximum length of the array to return. If this argument is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.
return value
An array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator. The string in the returned array does not include the separator itself.
However, if separator is a regular expression that contains a subexpression, the returned array includes a string that matches the subexpression (but does not include text that matches the entire regular expression).
Tips and comments
Note: If an empty string ("") is used as a separator, then each character in the Stringobject is split.
Note: The actions performed by String.Split () are the opposite of those performed by Array.join.
In this case, we'll split the string in different ways:
<script type= "Text/javascript" > var str= "How are your doing today?" document.write (Str.split ("") + "<br>") document.write (Str.split ("") + "<br>") document.write (Str.split (" ", 3)" </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Output:
Copy Code code as follows:

How,are,you,doing,today?
H,o,w, A,r,e, Y,o,u, D,o,i,n,g, T,o,d,a,y,?

how,are,you Example
In this case, we'll split the more complex string of structures:
"2:3:4:5". Split (":")//will return ["2", "3", "4", "5"]
"|a|b|c". Split ("|") will return ["", "a", "B", "C", ""]
Example 3
Using the following code, you can split a sentence into words:
var words = Sentence.split (') or using regular expressions as separator:
var words = Sentence.split (/\s+/) Example 4
If you want to split the word into letters, or split the string into characters, use the following code:
"Hello". Split ("")//May return ["H", "E", "L", "L", "O"] if you want to return only a subset of the characters, use the Howmany parameter:
"Hello". Split ("", 3)//May return ["H", "E", "L"]

About the use of JS split is not much else to say what, the following direct examples for everyone to see
<script language= "JavaScript" > str= "2,2,3,5,6,6"; This is a string var strs= new Array (); Defines an array of strs=str.split (","); Character segmentation for (i=0;i<strs.length; i++) {document.write (strs[i]+ "<br>");//split character output} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.