trimws
Remove Leading/Trailing Whitespace
Remove leading and/or trailing whitespace from character strings.
- Keywords
- character
Usage
trimws(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]")
Arguments
- x
a character vector
- which
a character string specifying whether to remove both leading and trailing whitespace (default), or only leading (
"left"
) or trailing ("right"
). Can be abbreviated.- whitespace
a string specifying a regular expression to match (one character of) “white space”, see Details for alternatives to the default.
Details
Internally, sub(re, "", *, perl = TRUE)
, i.e., PCRE
library regular expressions are used.
For portability, the default ‘whitespace’ is the character class
[ \t\r\n]
(space, horizontal tab, carriage return,
newline). Alternatively, [\h\v]
is a good (PCRE)
generalization to match all Unicode horizontal and vertical white
space characters, see also https://www.pcre.org.
Examples
library(base)
# NOT RUN {
x <- " Some text. "
x
trimws(x)
trimws(x, "l")
trimws(x, "r")
## Unicode --> need "stronger" 'whitespace' to match all :
tt <- "text with unicode 'non breakable space'."
xu <- paste(" \t\v", tt, "\u00a0 \n\r")
(tu <- trimws(xu, whitespace = "[\\h\\v]"))
stopifnot(identical(tu, tt))
# }
Community examples
a <- "\nrose" cat(a) a <- trimws(a) cat(a) b <- " rose " cat(b) b <- trimws(b,"left") cat(b) c <- "\trose " cat(c) c <- trimws(c,whitespace="[\t]") cat(c) cat("Thank you!")