R/str_begins.R

#' Detect strings beginning with some substring
#'
#' @description helpful to combine with `filter()`
#'
#' @param x character vector
#' @param pattern substring that you want to detect
#'
#' @examples
#' x <- c("C22240098", "C22250098", "C22240189", "G1003260")
#' x %>% str_begins("G")
#' x %>% str_begins("C22")
#'
#' library(tidyverse)
#' df <- tibble(src_code = x)
#' df %>% filter(src_code %>% str_begins("G"))
#' df %>% filter(src_code %>% str_begins("C22"))
#'
#' @export
str_begins <- function (x, pattern) {
  str_detect(x, str_c("^", pattern))
}
BAAQMD/strtools documentation built on May 3, 2024, 10:56 a.m.