String#split
str.split(pattern=nil, limit=nil) -> array Returns:
Array · Updated March 13, 2026 · String Methods strings split arrays parsing
The split method breaks a string into an array of substrings using a specified delimiter pattern. It’s one of the most frequently used string methods in Ruby, essential for parsing CSV data, processing text files, tokenizing user input, and manipulating delimited strings. When no pattern is provided, split defaults to splitting on whitespace while also collapsing consecutive whitespace characters and trimming leading/trailing empty strings.
Syntax
str.split(pattern=nil, limit=nil)
The method takes an optional pattern (string or regex) and an optional limit integer. Both parameters have sensible defaults when omitted.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern | String or Regexp | nil (whitespace) | The delimiter to split on. Can be a plain string, a regular expression, or nil to split on whitespace. |
limit | Integer | nil (unlimited) | Maximum number of elements to return. When positive, returns at most limit elements with the last element containing the remainder. When negative, returns all elements without collapsing trailing empty strings. |
Examples
Splitting on a String Delimiter
# Basic string split on comma
"apple,banana,cherry".split(",")
# => ["apple", "banana", "cherry"]
# Split on space
"hello world".split(" ")
# => ["hello", "world"]
# Split on multi-character delimiter
"one|||two|||three".split("|||")
# => ["one", "two", "three"]
Splitting with a Regex Pattern
# Split on one or more whitespace characters
"one two\nthree \tfour".split(/\s+/)
# => ["one", "two", "three", "four"]
# Split on digits
"abc123def456ghi".split(/\d+/)
# => ["abc", "def", "ghi"]
# Split on multiple delimiters (regex alternation)
# "a,b;c:d".split(/[,:;]/)
# => ["a", "b", "c", "d"]
Using the Limit Parameter
# Limit to first N elements
"a,b,c,d,e".split(",", 3)
# => ["a", "b", "c,d,e"]
# Negative limit keeps trailing empty strings
"a,b,c,,".split(",", -1)
# => ["a", "b", "c", "", ""]
# Default behavior (no limit) removes trailing empties
"a,b,c,,".split(",")
# => ["a", "b", "c"]
# Limit of 1 returns the whole string as single element
"hello world".split(" ", 1)
# => ["hello world"]
Splitting Without Arguments
# No arguments = split on whitespace
" one two three ".split
# => ["one", "two", "three"]
# Multiple spaces/newlines are treated as single delimiter
"one\n\n\ntwo\t\tthree".split
# => ["one", "two", "three"]
Common Patterns
Parsing CSV Data
# Simple CSV parsing
csv_line = "John,Doe,30,New York"
fields = csv_line.split(",")
# => ["John", "Doe", "30", "New York"]
# Handling quoted fields requires a more complex regex
# but split gets you started quickly
Tokenizing User Input
# Split sentence into words
sentence = "The quick brown fox jumps"
words = sentence.split
# => ["The", "quick", "brown", "fox", "jumps"]
# Get first N words only
first_two = sentence.split[0..1]
# => ["The", "quick"]
Processing Log Files
# Split log entry on pipe character
log_entry = "2024-01-15|ERROR|Connection timeout"
parts = log_entry.split("|")
date, level, message = parts
# date = "2024-01-15", level = "ERROR", message = "Connection timeout"
Extracting Numbers from Strings
# Pull all numbers out of a string
text = "Order #12345 for $99.99"
numbers = text.split(/[^0-9]+/).reject(&:empty?)
# => ["12345", "99", "99"]
Splitting by Newlines
# Split text into lines
multiline = "line one\nline two\nline three"
lines = multiline.split("\n")
# => ["line one", "line two", "line three"]
# Or use regex for cross-platform compatibility
lines = multiline.split(/\r?\n/)
# => ["line one", "line two", "line three"]
Errors
ArgumentError
# Limit must be an integer
"a,b,c".split(",", "three")
# => ArgumentError: limit must be an integer
RegexpError
# Invalid regex pattern
"test".split(/[/)
# => RegexpError: unmatched close parenthesis
Special Cases with Empty Strings
# Splitting empty string
"".split(",")
# => []
# Splitting on empty string (Ruby 3.1+)
# "hello".split("")
# => ["h", "e", "l", "l", "o"]
# In older Ruby versions, this behavior differs
# Splitting with limit 0 returns nil
# Note: limit 0 is treated specially and returns nil
# "a,b,c".split(",", 0)
# => nil
See Also
- string::join — The inverse of split; joins array elements into a string
- string::scan — Extracts all matches of a pattern into an array
- array::flatten — Flattens nested arrays after splitting