October 2006
Intermediate to advanced
888 pages
16h 55m
English
The split method parses a string and returns an array of tokens. It accepts two parameters, a delimiter and a field limit (which is an integer).
The delimiter defaults to whitespace. Actually, it uses $; or the English equivalent $FIELD_SEPARATOR. If the delimiter is a string, the explicit value of that string is used as a token separator.
s1 = "It was a dark and stormy night."
words = s1.split # ["It", "was", "a", "dark", "and",
# "stormy", "night"]
s2 = "apples, pears, and peaches"
list = s2.split(", ") # ["apples", "pears", "and peaches"]
s3 = "lions and tigers and bears"
zoo = s3.split(/ and /) # ["lions", "tigers", "bears"]The limit parameter places an upper limit on the number of fields returned, according to these ...
Read now
Unlock full access