split
![Extensive documentation Importance_4](https://d2vfyqvduarcvs.cloudfront.net/images/importance_4.png?1349367920)
split(p1 = v1, p2 = v2)
public
Divides str into substrings based on a delimiter, returning an array of these substrings.
If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading and trailing whitespace and runs of contiguous whitespace characters ignored.
If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.
If pattern is nil, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ‘ ’ were specified.
If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of split substrings will be returned (captured groups will be returned as well, but are not counted towards the limit). If limit is 1, the entire string is returned as the only entry in an array. If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
When the input str is empty an empty Array is returned as the string is considered to have no fields to split.
" now's the time ".split #=> ["now's", "the", "time"] " now's the time ".split(' ') #=> ["now's", "the", "time"] " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"] "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"] "hello".split(//) #=> ["h", "e", "l", "l", "o"] "hello".split(//, 3) #=> ["h", "e", "llo"] "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"] "mellow yellow".split("ello") #=> ["m", "w y", "w"] "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"] "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"] "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""] "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"] "".split(',', -1) #=> []
If a block is given, invoke the block with each split substring.
![Default_avatar_30](https://www.gravatar.com/avatar/49b6123803e4f327144e991daab62f77?size=30&default=http://apidock.com/images/default_avatar_30.png)
Regexes with groups and split
When you use a Regex with capture groups, all capture groups are included in the results (interleaved with the “real” results) but they do not count for the limit argument.
Examples:
"abc.,cde.,efg.,ghi".split(/.(,)/) => ["abc", ",", "cde", ",", "efg", ",", "ghi"] "abc.,cde.,efg.,ghi".split(/(.)(,)/) => ["abc", ".", ",", "cde", ".", ",", "efg", ".", ",", "ghi"] "abc.,cde.,efg.,ghi".split(/(.(,))/) => ["abc", ".,", ",", "cde", ".,", ",", "efg", ".,", ",", "ghi"] "abc.,cde.,efg.,ghi".split(/(.(,))/, 2) => ["abc", ".,", ",", "cde.,efg.,ghi"] "abc.,cde.,efg.,ghi".split(/(.(,))/, 3) => ["abc", ".,", ",", "cde", ".,", ",", "efg.,ghi"]
![Default_avatar_30](https://www.gravatar.com/avatar/6dd10772cfa5333c5db924091464ed95?size=30&default=http://apidock.com/images/default_avatar_30.png)
The reverse operation of split is join.
Given that String#split returns an array, its reverse operation is Array#join. Example:
"life is awesome".split =>["life","is","awesome"] ["life","is","awesome"].join(" ") =>"life is awesome"
![Default_avatar_30](https://www.gravatar.com/avatar/94afc1c045c917bd7a9b8efd5822dde2?size=30&default=http://apidock.com/images/default_avatar_30.png)
clarification of inputs
“split(p1 = v1, p2 = v2)”
in reading the rest of the documentation, i found “p1” and “p2” to be confusing.
I think it should be:
split( pattern, limit )