str_split

0th

Percentile

Split up a string into a variable number of pieces.

Split up a string into a variable number of pieces.

Keywords
character
Usage
str_split(string, pattern, n = Inf)
Arguments
string
input character vector
pattern
pattern to split up by, as defined by a POSIX regular expression. See the ``Extended Regular Expressions'' section of regex for details. If NA, returns original string. If "" spli
n
maximum number of pieces to return. Default (Inf) uses all possible split positions.
Details

Vectorised over string. pattern should be a single pattern, i.e. a character vector of length one.

Value

  • a list of character vectors.

See Also

str_split_fixed for fixed number of splits

Aliases
  • str_split
Examples
fruits <- c(
"apples and oranges and pears and bananas",
"pineapples and mangos and guavas"
)
str_split(fruits, "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)
Documentation reproduced from package stringr, version 0.5, License: GPL-2

Community examples

rachhpal1176 at Jan 31, 2019 stringr v1.3.1

z <- 'World, Asia, India, Punjab and Jalandhar' # Option 1 str_split(z, ",\\s|\\sand\\s") # Option 2 str_split(z, ", | and ")

chem.mph at Jul 26, 2017 stringr v1.1.0

```r datetime = "01/01/1900 00:00:00" #Displays split date #Splits string by space str_split(date, " ", simplify = TRUE) #Reference individual parts #Extracts date str_split(date, " ", simplify = TRUE)[,1] #Extracts time str_split(date, " ", simplify = TRUE)[,2] #Extracting year presents a problem when splitting by / #time is included with year str_split(date, "/", simplify = TRUE)[,3] #Extracting year only strtrim(str_split(date, "/", simplify = TRUE)[,3],4) ```