In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use split in javascript. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The function of javascript split is to split a string into an array of strings, and its syntax is "stringObject.split (separator,howmany)".
This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.
What is the use of javascript split?
The split () method is used to split a string into an array of strings.
Grammar
StringObject.split (separator,howmany)
Parameters.
Separator is required. A string or regular expression that splits the stringObject from the place specified by the parameter.
Howmany is optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter 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 separator itself.
However, if separator is a regular expression that contains subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).
Tips and comments
Note: if an empty string ("") is used as a separator, each character in the stringObject will be split.
Note: the action performed by String.split () is the opposite of that performed by Array.join.
Example
Example 1
In this example, we will split the string in different ways:
Var str= "How are you doing today?" [xss_clean] (str.split ("") + ") [xss_clean] (str.split (") + ") [xss_clean] (str.split (", 3))
Output:
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 2
In this example, we will split a string with a more complex structure:
"2JV 3VL 4RV 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 divide a sentence into words:
Var words = sentence.split ('')
Or use a regular expression as the separator:
Var words = sentence.split (/\ swords /)
Example 4
If you want to split a word into letters, or a string into characters, use the following code:
"hello" .split (") / / can return [" h "," e "," l "," l "," o "]
To return only a portion of the characters, use the howmany parameter:
"hello" .split (", 3) / / return [" h "," e "," l "] these are all the contents of the article" how to use split in javascript ". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.