R/string_switchletter.R

Defines functions string_switchletter

Documented in string_switchletter

#' Switch a single letter in a string with a new letter
#'
#' Switch a single letter in a character string that is surrounded by
#' either full stops, underscores, or dashes with a new letter.
#' This is helpful when a file path has a single letter for area or
#' some other category and you want to easily switch it out with a new
#' letter. For example, "apple-a-sweet" can easily be changed to
#' "apple-b-sweet" with this function.
#'
#' @param x A character string.
#' @param newletter A character string of any length you want, but
#' only the first letter will be used to replace the value in `x`.
#' Note that we do not pay attention to case here and all new letters
#' will be lower-case.
#'
#' @author Kelli F. Johnson
string_switchletter <- function(x, newletter) {
  if (newletter == "") {
    return(gsub(
      "[\\._-][a-zA-Z][\\._-]",
      "",
      x
    ))
  }
  arealetter <- substr(tolower(newletter), 1, 1)
  return(gsub(
    "([\\._-])[a-z]([\\._-])",
    paste0("\\1", arealetter, "\\1"),
    x
  ))
}
iantaylor-NOAA/Lingcod_2021 documentation built on Oct. 30, 2024, 6:42 p.m.