Skip to content
Closed
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export(str_replace_na)
export(str_sort)
export(str_split)
export(str_split_fixed)
export(str_squish)
export(str_sub)
export(str_subset)
export(str_to_lower)
Expand Down
14 changes: 14 additions & 0 deletions R/pad-trim.r
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ str_trim <- function(string, side = c("both", "left", "right")) {
both = stri_trim_both(string)
)
}

#' Trim whitespace from start, middle, and end of string.
#'
#' @param string A character vector.
#' @return A character vector.
#' @export
#' @seealso \code{\link{str_trim}} to see end-only trimming
#' @examples
#' str_squish(" String with trailing, middle, and leading white space\t")
#' str_squish("\n\nString with excess, trailing and leading white space\n\n")
str_squish <- function(string) {
stri_trim_both(str_replace_all(string,"\\s+"," "))
}

24 changes: 24 additions & 0 deletions man/str_squish.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-trim.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ test_that("side argument restricts trimming", {
expect_equal(str_trim(" abc ", "left"), "abc ")
expect_equal(str_trim(" abc ", "right"), " abc")
})

test_that("squishing all removes all excess spaces and tabs", {
expect_equal(str_squish("ab\t\tc\t"), "ab c")
expect_equal(str_squish("\ta bc"), "a bc")
expect_equal(str_squish("\ta\t bc\t"), "a bc")
})