Get substring from string when substring is defined in vector
Let's say I have a vector like this
foo <- c('est','bel','cat')
and then I have a line like:
str <- "test"
How can I get "est" for a refund
+3
Ibi taher
source
to share
2 answers
library(stringr)
foo[str_detect(str, foo)]
#> [1] "est"
And when multiple valid substrings appear:
foo <- c('est','bel','cat', 'tes')
foo[str_detect(str, foo)]
#> [1] "est" "tes"
+1
Simon jackson
source
to share
which(sapply(foo, (function(x) grepl(x, str)))==T)
0
TheBiro
source
to share