Created
November 4, 2015 11:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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