← Reference · Nestor G Pestelos Jr · Print this page
Programming Languages · Ruby
Ruby String Splitting
Reference entry · last updated September 21, 2026
Ruby string splitting is String#split, which divides a string at a separator and returns an Array of substrings. The separator is either a literal String or a Regexp, and those two behave differently. An optional limit caps the number of fields. The result distinguishes "no separator was found" from "the separator was found and the field is empty".[1]
First principles and definitions
Two pattern types
The pattern decides the algorithm. A String pattern is matched literally: every character is ordinary, so regex metacharacters lose their meaning. A Regexp pattern is matched as a regular expression, and the string is divided wherever it matches.[1]
"THX1138".split(/\d+/, 2) # => ["THX", ""]
"THX1138".split('\d+', 2) # => ["THX1138"]
The first call splits on one or more digits. The second looks for the literal characters backslash, d, plus, finds none, and returns the whole string as one element. Single quotes keep the backslash; a double-quoted "\d+" drops it to "d+".
The whitespace default
With no argument, the pattern is nil, and the value of $; is used. Because $; is nil by default, the string is split on whitespace as if a single space had been passed. That form ignores leading and trailing whitespace and treats a run of whitespace as one separator.[1]
" now the time ".split # => ["now", "the", "time"]
"a\nb".split("\n") # => ["a", "b"]
"".split # => []
An empty string returns an empty Array, because there are no fields to split.
The limit argument
The optional second argument caps how many fields are returned. It also controls trailing empty fields.[1]
| Limit | Effect |
|---|---|
| omitted | trailing empty fields are suppressed |
| positive n | at most n fields plus any captured groups; the last field keeps the unsplit remainder |
1 | the whole string as a one-element Array |
| negative | no field limit, and trailing empty fields are kept |
"a,b,c".split(",", 2) # => ["a", "b,c"]
"a,b,,".split(",") # => ["a", "b"]
"a,b,,".split(",", -1) # => ["a", "b", "", ""]
One exception: when the pattern contains a capture group, the captured groups are inserted into the result as well, and they do not count toward the limit.[1]
"a,b,c".split(/(,)/, 2) # => ["a", ",", "b,c"]
The limit is a separate argument to split, not part of the pattern. Writing it inside the regex, as in split(/\s*=\s*, 2/), makes it pattern text and also leaves the regex unterminated if the closing slash is lost.
Results and edge cases
With a limit of 2, the Array length carries information that a multiple assignment hides. A one-element result means no separator matched, so a second variable receives nil. A two-element result with an empty second string means the separator matched and the field after it is empty.[1]
line = "badline" key, value = line.split(/=/, 2) # key "badline", value nil line = "a=" key, value = line.split(/=/, 2) # key "a", value ""
That reading depends on the limit. With the default limit, trailing empty fields are suppressed, so "a=".split(/=/) returns ["a"] even though the separator matched. The limit of 2 keeps the empty field and makes the distinction visible.
Those are different cases: nil is "there was no separator", while "" is "there was a separator with nothing after it". A guard written as value.nil? || value.empty? conflates them and drops valid empty fields.
Choosing a method
- Words separated by any whitespace:
str.split. - A fixed delimiter, keep the remainder:
str.split("=", 2). - A pattern:
str.split(/\s*=\s*/). - Keep trailing empty fields: a negative limit.
- Only the first separator, with a fixed three-part result:
str.partition("="). - Remove edge characters rather than divide the string: see Ruby String Tokenization.
References
- ^ Ruby 3.3 core documentation, "String,"
split. Free full text: docs.ruby-lang.org/en/3.3/String.html - ^ Ruby 3.3 core documentation, "String,"
partition. Free full text: docs.ruby-lang.org/en/3.3/String.html