This functions checks whether a string or character vector
x
contains the string pattern
. By default,
this function is case sensitive.
str_contains(x, pattern, ignore.case = FALSE, logic = NULL, switch = FALSE)
Character string where matches are sought. May also be a character vector of length > 1 (see 'Examples').
Character string to be matched in x
. May also be a
character vector of length > 1 (see 'Examples').
Logical, whether matching should be case sensitive or not.
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 of pattern
is in x
.
Use "and"
, "AND"
or "&"
for a logical AND-combination, i.e. all elements of pattern
are in x
.
Use "not"
, "NOT"
or "!"
for a logical NOT-combination, i.e. no element of pattern
is in x
.
By default, logic = NULL
, which means that TRUE
or FALSE
is returned for each element of pattern
separately.
Logical, if TRUE
, x
will be sought in each element
of pattern
. If switch = TRUE
, x
needs to be of
length 1.
TRUE
if x
contains pattern
.
This function iterates all elements in pattern
and
looks for each of these elements if it is found in
any element of x
, i.e. which elements
of pattern
are found in the vector x
.
Technically, it iterates pattern
and calls
grep(x, pattern[i], fixed = TRUE)
for each element
of pattern
. If switch = TRUE
, it iterates
pattern
and calls grep(pattern[i], x, fixed = TRUE)
for each element of pattern
. Hence, in the latter case
(if switch = TRUE
), x
must be of length 1.
str_contains("hello", "hel")
#> [1] TRUE
str_contains("hello", "hal")
#> [1] FALSE
str_contains("hello", "Hel")
#> [1] FALSE
str_contains("hello", "Hel", ignore.case = TRUE)
#> [1] TRUE
# which patterns are in "abc"?
str_contains("abc", c("a", "b", "e"))
#> [1] TRUE TRUE FALSE
# is pattern in any element of 'x'?
str_contains(c("def", "abc", "xyz"), "abc")
#> [1] TRUE
# is "abcde" in any element of 'x'?
str_contains(c("def", "abc", "xyz"), "abcde") # no...
#> [1] FALSE
# is "abc" in any of pattern?
str_contains("abc", c("defg", "abcde", "xyz12"), switch = TRUE)
#> [1] FALSE TRUE FALSE
str_contains(c("def", "abcde", "xyz"), c("abc", "123"))
#> [1] TRUE FALSE
# any pattern in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "or")
#> [1] TRUE
# all patterns in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "and")
#> [1] FALSE
str_contains("abc", c("a", "b"), logic = "and")
#> [1] TRUE
# no patterns in "abc"?
str_contains("abc", c("a", "b", "e"), logic = "not")
#> [1] FALSE
str_contains("abc", c("d", "e", "f"), logic = "not")
#> [1] TRUE