strsplit()
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE) Returns:
list · Updated March 13, 2026 · String Functions string split text base
strsplit() splits a character vector into substrings by finding matches of a delimiter pattern. It returns a list where each element contains the substrings from the corresponding element of the input.
Syntax
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | character | — | A character vector to split |
split | character | — | The pattern to split on (regular expression unless fixed = TRUE) |
fixed | logical | FALSE | If TRUE, match split literally rather than as a regex |
perl | logical | FALSE | If TRUE, use Perl-compatible regular expressions |
useBytes | logical | FALSE | If TRUE, match bytes rather than characters |
Examples
Basic usage
# Split by space
text <- "hello world"
strsplit(text, " ")
# [[1]]
# [1] "hello" "world"
# Split by comma
colors <- "red,green,blue,orange"
strsplit(colors, ",")
# [[1]]
# [1] "red" "green" "blue" "orange"
Splitting into characters
# Split each character
word <- "abcdef"
strsplit(word, "")
# [[1]]
# [1] "a" "b" "c" "d" "e" "f"
Using fixed = TRUE for literal matching
# Split on a literal period (not regex)
paths <- c("file.txt", "data.csv", "script.R")
strsplit(paths, ".", fixed = TRUE)
# [[1]]
# [1] "file" "txt"
# [[2]]
# [1] "data" "csv"
# [[3]]
# [1] "script" "R"
Using regular expressions
# Split on whitespace (one or more spaces)
sentence <- "The quick brown fox"
strsplit(sentence, "\\s+")
# [[1]]
# [1] "The" "quick" "brown" "fox"
# Split on digits
text <- "abc123def456"
strsplit(text, "[0-9]+")
# [[1]]
# [1] "abc" "def" ""
Common Patterns
First element only
# Get first split element
parts <- strsplit("first,second,third", ",")[[1]]
parts[1]
# [1] "first"
Combine with sapply
# Split multiple strings and get lengths
words <- c("a b c", "d e f g", "h i")
sapply(strsplit(words, " "), length)
# [1] 3 4 2
Data cleaning with strsplit and paste
# Remove extra whitespace
dirty <- " hello world "
clean <- paste(strsplit(dirty, "\\s+")[[1]], collapse = " ")
clean
# [1] "hello world"