R: How to ignore case when using str_detect?

You can use regex (or fix as @lmo's comments depending on what you need) function to make the pattern as detailed in ?modifiers or ?str_detect (see the instruction for pattern parameter):

library(stringr)
str_detect('TOYOTA subaru', regex('toyota', ignore_case = T))
# [1] TRUE

the search string must be inside function fixed and that function has valid parameter ignore_case

str_detect('TOYOTA subaru', fixed('toyota', ignore_case=TRUE))

You can save a little typing with (?i):

c("Toyota", "my TOYOTA", "your Subaru") %>% 
  str_detect( "(?i)toyota" )
# [1]  TRUE  TRUE FALSE

Tags:

R

Stringr