Skip to content

Instantly share code, notes, and snippets.

@swayson
Created November 4, 2015 11:51
Show Gist options
  • Save swayson/559e99c6d076184d7472 to your computer and use it in GitHub Desktop.
Save swayson/559e99c6d076184d7472 to your computer and use it in GitHub Desktop.
Utility functions to easily check if a strings starts or ends with a given pattern
starts_with <- function(vars, match, ignore.case = TRUE) {
if (ignore.case) match <- tolower(match)
n <- nchar(match)
if (ignore.case) vars <- tolower(vars)
substr(vars, 1, n) == match
}
ends_with <- function(vars, match, ignore.case = TRUE) {
if (ignore.case) match <- tolower(match)
n <- nchar(match)
if (ignore.case) vars <- tolower(vars)
length <- nchar(vars)
substr(vars, pmax(1, length - n + 1), length) == match
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment