diff --git a/NAMESPACE b/NAMESPACE index 6bf6f83f..7d63581d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,8 @@ export(str_match) export(str_match_all) export(str_order) export(str_pad) +export(str_remove) +export(str_remove_all) export(str_replace) export(str_replace_all) export(str_replace_na) diff --git a/NEWS.md b/NEWS.md index 98317030..efa4af9f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,10 @@ * `str_trunc()` now preserves NAs (@ClaytonJY, #162) +* New `str_remove()` and `str_remove_all()` functions. These wrap + `str_replace()` and `str_replace_all()` to remove patterns from strings. + (@Shians, #178) + # stringr 1.2.0 ## API changes diff --git a/R/remove.r b/R/remove.r new file mode 100644 index 00000000..8fdc5a38 --- /dev/null +++ b/R/remove.r @@ -0,0 +1,22 @@ +#' Remove matched patterns in a string. +#' +#' Alias for str_replace(string, pattern, ""). +#' +#' @inheritParams str_detect +#' +#' @return A character vector. +#' @seealso \code{\link{str_replace}} for the underlying implementation. +#' @export +#' @examples +#' fruits <- c("one apple", "two pears", "three bananas") +#' str_remove(fruits, "[aeiou]") +#' str_remove_all(fruits, "[aeiou]") +str_remove <- function(string, pattern) { + str_replace(string, pattern, "") +} + +#' @export +#' @rdname str_remove +str_remove_all <- function(string, pattern) { + str_replace_all(string, pattern, "") +} \ No newline at end of file diff --git a/man/str_remove.Rd b/man/str_remove.Rd new file mode 100644 index 00000000..261c8113 --- /dev/null +++ b/man/str_remove.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/remove.r +\name{str_remove} +\alias{str_remove} +\alias{str_remove_all} +\title{Remove matched patterns in a string.} +\usage{ +str_remove(string, pattern) + +str_remove_all(string, pattern) +} +\arguments{ +\item{string}{Input vector. Either a character vector, or something +coercible to one.} + +\item{pattern}{Pattern to look for. + + The default interpretation is a regular expression, as described + in \link[stringi]{stringi-search-regex}. Control options with + \code{\link{regex}()}. + + Match a fixed string (i.e. by comparing only bytes), using + \code{\link{fixed}(x)}. This is fast, but approximate. Generally, + for matching human text, you'll want \code{\link{coll}(x)} which + respects character matching rules for the specified locale. + + Match character, word, line and sentence boundaries with + \code{\link{boundary}()}. An empty pattern, "", is equivalent to + \code{boundary("character")}.} +} +\value{ +A character vector. +} +\description{ +Alias for str_replace(string, pattern, ""). +} +\examples{ +fruits <- c("one apple", "two pears", "three bananas") +str_remove(fruits, "[aeiou]") +str_remove_all(fruits, "[aeiou]") +} +\seealso{ +\code{\link{str_replace}} for the underlying implementation. +} diff --git a/tests/testthat/test-remove.r b/tests/testthat/test-remove.r new file mode 100644 index 00000000..9eaadeba --- /dev/null +++ b/tests/testthat/test-remove.r @@ -0,0 +1,6 @@ +context("Removal") + +test_that("basic removal works", { + expect_equal(str_remove_all("abababa", "ba"), "a") + expect_equal(str_remove("abababa", "ba"), "ababa") +})