Learn R Programming

sjmisc (version 1.7)

str_contains: Check if string contains pattern

Description

This functions checks whether a string x contains the string pattern. By default, this function is case sensitive.

Usage

str_contains(x, pattern, ignore.case = FALSE, logic = NULL)

Arguments

x
Character string where matches are sought.
pattern
Character string to be matched in x. May also be a character vector of length > 1 (see 'Examples').
ignore.case
Logical, whether matching should be case sensitive or not.
logic
Indicates whether a logical combination of multiple search pattern should be made.
  • Use"or","OR"or"|"for a logical or-combination, i.e. at least one element ofpatternis inx.
  • U

Value

  • TRUE if x contains pattern.

Examples

Run this code
str_contains("hello", "hel")
str_contains("hello", "hal")

str_contains("hello", "Hel")
str_contains("hello", "Hel", ignore.case = TRUE)

# which patterns are in "abc"?
str_contains("abc", c("a", "b", "e"))

# any pattern in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "or")

# all patterns in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "and")
str_contains("abc", c("a", "b"), logic = "and")

# no patterns in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "not")
str_contains("abc", c("d", "e", "f"), logic = "not")

Run the code above in your browser using DataLab