R/contains.R

Defines functions strs_contains

Documented in strs_contains

#' Check if string contains a substring
#'
#' `strs_contains` checks whether each element of a character vector contains a
#' specified substring. This function mirrors the functionality of Python's
#' `str.__contains__()` method.
#'
#' @param string A character vector where each element is a string to be
#' checked.
#' @param substring The substring to search for within each element of `string`.
#' @return A logical vector of the same length as `string`, with each element
#' indicating whether the corresponding element of `string` contains
#' `substring`.
#' @examples
#' strs_contains("hello world", "world")
#' strs_contains(c("apple", "banana", "cherry"), "a")
#' @export
strs_contains <- function(string, substring) {
  stringi::stri_detect_fixed(string, substring)
}

Try the strs package in your browser

Any scripts or data that you put into this service are public.

strs documentation built on Sept. 11, 2024, 6:44 p.m.