Skip to contents

Vectorised over string and pattern.

Usage

str_extract_part(string, pattern, before = TRUE)

Arguments

string

A character vector.

pattern

Pattern to look for.

before

The position in the string to extract from. If TRUE, the extract will occur before the pattern; if FALSE, it will happen after the pattern.

Value

A subset of the input vector.

See also

str_split_extract() which splits up a string into pieces and extracts the results using a specified index position.

Examples


weekdays <- c(
  "Monday_1", "Tuesday_2", "Wednesday_3", "Thursday_4",
  "Friday_5", "Saturday_6", "Sunday_7"
)

str_extract_part(weekdays, before = TRUE, pattern = "_")
#> [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
#> [7] "Sunday"   

str_extract_part(c("$159", "$587", "$897"), before = FALSE, pattern = "$")
#> [1] "159" "587" "897"