R: How to ignore case when using str_detect?

userJT picture userJT · Jun 13, 2017 · Viewed 19.6k times · Source

stringr package provides good string functions.

To search for a string (ignoring case)

one could use

stringr::str_detect('TOYOTA subaru',ignore.case('toyota'))

This works but gives warning

Please use (fixed|coll|regex)(x, ignore_case = TRUE) instead of ignore.case(x)

What is the right way of rewriting it?

Answer

Psidom picture Psidom · Jun 13, 2017

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