Vectorised over `string`

and `pattern`

.

str_split(string, pattern, n = Inf, simplify = FALSE)
str_split_fixed(string, pattern, n)
str_split_n(string, pattern, n)

## 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 stringi::stringi-search-regex. Control options with
`regex()` .
Match a fixed string (i.e. by comparing only bytes), using
`fixed()` . This is fast, but approximate. Generally,
for matching human text, you'll want `coll()` which
respects character matching rules for the specified locale.
Match character, word, line and sentence boundaries with
`boundary()` . An empty pattern, "", is equivalent to
`boundary("character")` . |

n |
number of pieces to return. Default (Inf) uses all
possible split positions.
For `str_split_fixed` , if `n` is greater than the number of pieces,
the result will be padded with empty strings.
For `str_split_n` , `n` is the desired index of each element of
the split `string` . When there are fewer pieces than `n` , return `NA` . |

simplify |
If `FALSE` , the default, returns a list of character
vectors. If `TRUE` returns a character matrix. |

## Value

For `str_split_fixed`

, a character matrix with `n`

columns.
For `str_split`

, a list of character vectors. For `str_split_n`

,
a length `n`

character vector.

## See also

`stri_split()`

for the underlying implementation.

## Examples

#> [[1]]
#> [1] "apples" "oranges" "pears" "bananas"
#>
#> [[2]]
#> [1] "pineapples" "mangos" "guavas"
#>

str_split(fruits, " and ", simplify = TRUE)

#> [,1] [,2] [,3] [,4]
#> [1,] "apples" "oranges" "pears" "bananas"
#> [2,] "pineapples" "mangos" "guavas" ""

# Specify n to restrict the number of possible matches
str_split(fruits, " and ", n = 3)

#> [[1]]
#> [1] "apples" "oranges" "pears and bananas"
#>
#> [[2]]
#> [1] "pineapples" "mangos" "guavas"
#>

str_split(fruits, " and ", n = 2)

#> [[1]]
#> [1] "apples" "oranges and pears and bananas"
#>
#> [[2]]
#> [1] "pineapples" "mangos and guavas"
#>

# If n greater than number of pieces, no padding occurs
str_split(fruits, " and ", n = 5)

#> [[1]]
#> [1] "apples" "oranges" "pears" "bananas"
#>
#> [[2]]
#> [1] "pineapples" "mangos" "guavas"
#>

# Use fixed to return a character matrix
str_split_fixed(fruits, " and ", 3)

#> [,1] [,2] [,3]
#> [1,] "apples" "oranges" "pears and bananas"
#> [2,] "pineapples" "mangos" "guavas"

str_split_fixed(fruits, " and ", 4)

#> [,1] [,2] [,3] [,4]
#> [1,] "apples" "oranges" "pears" "bananas"
#> [2,] "pineapples" "mangos" "guavas" ""

# str_split_n extracts only a single piece from a string
str_split_n(fruits, " and ", 1)

#> [1] "apples" "pineapples"

str_split_n(fruits, " and ", 3)

#> [1] "pears" "guavas"