Skip to contents

str_split_first(), str_split_nth() and str_split_last(): take the first, nth and last result of splitting each string in the input vector based on a pattern delimiter.

str_split_all() splits each string in the input vector into substrings based on a pattern.

chr_split_all() splits all strings in the input vector and returns the substrings in a single character vector.

Usage

str_split_all(strings, pattern, fixed = FALSE)

str_split_first(strings, pattern, fixed = FALSE)

str_split_nth(strings, pattern, n, fixed = FALSE)

str_split_last(strings, pattern, fixed = FALSE)

chr_split_all(strings, pattern, fixed = FALSE)

Arguments

strings

A character vector, where each element of the vector is a character string.

pattern

A single character string to be searched for in each element of strings. By default, pattern is interpreted as a regular expression (regex). If the fixed argument is set to TRUE, pattern will be treated as a literal string to be matched exactly.

fixed

Logical; whether pattern should be matched exactly, treating regex special characters as regular string characters. Default FALSE.

n

(str_split_nth only) Integer, the index of a substring to extract from a split string. Negative values count back from the end.

Value

str_split_first(), str_split_nth() and str_split_last(): A character vector the same length as strings, with each element being the first, nth or last substring obtained by splitting the corresponding element of strings.

str_split_all(): A list of the same length as strings, with each element being a character vector of substrings obtained by splitting the corresponding element of strings.

chr_split_all(): A single character vector containing all substrings obtained by splitting each element of strings. Equivalent of using unlist() on the output of str_split_all().

Details

These functions are built using the base R regular expression functions. {suitestrings} uses Perl-compatible Regular Expressions (PCRE). This is achieved by setting perl = TRUE in the underlying base functions. See R's regexp documentation for info on the regex implementation. For complete syntax details see https://www.pcre.org/current/doc/html/

See also

strsplit() which these functions wrap around.

Examples


str_split_first(c("one,two,three", "abc,def,ghi"), ",")
#> [1] "one" "abc"
str_split_nth(c("one,two,three", "abc,def,ghi"), ",", 2)
#> [1] "two" "def"
str_split_last(c("one,two,three", "abc,def,ghi"), ",")
#> [1] "three" "ghi"

str_split_all(c("one,two,three", "abc,def,ghi"), ",")
#> [[1]]
#> [1] "one"   "two"   "three"
#>
#> [[2]]
#> [1] "abc" "def" "ghi"
chr_split_all(c("one,two,three", "abc,def,ghi"), ",")
#> [1] "one"   "two"   "three" "abc"   "def"   "ghi"