Check if string contains pattern
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)
x |
Character string where matches are sought. May also be a character vector of length > 1 (see 'Examples'). |
pattern |
Character string to be matched in |
ignore.case |
Logical, whether matching should be case sensitive or not. |
logic |
Indicates whether a logical combination of multiple search pattern should be made.
|
switch |
Logical, if |
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.
TRUE
if x
contains pattern
.
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")) # is pattern in any element of 'x'? str_contains(c("def", "abc", "xyz"), "abc") # is "abcde" in any element of 'x'? str_contains(c("def", "abc", "xyz"), "abcde") # no... # is "abc" in any of pattern? str_contains("abc", c("defg", "abcde", "xyz12"), switch = TRUE) str_contains(c("def", "abcde", "xyz"), c("abc", "123")) # 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")
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.