str_split {stringr} | R Documentation |
Split up a string into pieces
Description
This family of functions provides various ways of splitting a string up into pieces. These two functions return a character vector:
-
str_split_1()
takes a single string and splits it into pieces, returning a single character vector. -
str_split_i()
splits each string in a character vector into pieces and extracts thei
th value, returning a character vector.
These two functions return a more complex object:
-
str_split()
splits each string in a character vector into a varying number of pieces, returning a list of character vectors. -
str_split_fixed()
splits each string in a character vector into a fixed number of pieces, returning a character matrix.
Usage
str_split(string, pattern, n = Inf, simplify = FALSE)
str_split_1(string, pattern)
str_split_fixed(string, pattern, n)
str_split_i(string, pattern, i)
Arguments
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression, as described in
Match a fixed string (i.e. by comparing only bytes), using
Match character, word, line and sentence boundaries with
|
n |
Maximum number of pieces to return. Default (Inf) uses all possible split positions. For |
simplify |
A boolean.
|
i |
Element to return. Use a negative value to count from the right hand side. |
Value
-
str_split_1()
: a character vector. -
str_split()
: a list the same length asstring
/pattern
containing character vectors. -
str_split_fixed()
: a character matrix withn
columns and the same number of rows as the length ofstring
/pattern
. -
str_split_i()
: a character vector the same length asstring
/pattern
.
See Also
stri_split()
for the underlying implementation.
Examples
fruits <- c(
"apples and oranges and pears and bananas",
"pineapples and mangos and guavas"
)
str_split(fruits, " and ")
str_split(fruits, " and ", simplify = TRUE)
# If you want to split a single string, use `str_split_1`
str_split_1(fruits[[1]], " and ")
# Specify n to restrict the number of possible matches
str_split(fruits, " and ", n = 3)
str_split(fruits, " and ", n = 2)
# If n greater than number of pieces, no padding occurs
str_split(fruits, " and ", n = 5)
# Use fixed to return a character matrix
str_split_fixed(fruits, " and ", 3)
str_split_fixed(fruits, " and ", 4)
# str_split_i extracts only a single piece from a string
str_split_i(fruits, " and ", 1)
str_split_i(fruits, " and ", 4)
# use a negative number to select from the end
str_split_i(fruits, " and ", -1)