Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions R/remove.r
Original file line number Diff line number Diff line change
@@ -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, "")
}
44 changes: 44 additions & 0 deletions man/str_remove.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-remove.r
Original file line number Diff line number Diff line change
@@ -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")
})