R: How to ignore case when using str_detect?

匆匆过客 提交于 2019-12-03 11:34:51

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 use the base R function grepl() to accomplish the same thing without a nested function. It simply accepts ignore.case as an argument.

grepl("toyota", 'TOYOTA subaru', ignore.case = TRUE)

(Note that the order of the first two arguments (pattern and string) are switched between grepl and str_detect).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!