Commit f2b2d239 authored by Radford Neal's avatar Radford Neal

added trimws

parent 46d70b92
......@@ -254,6 +254,9 @@
which uses the basic C function \code{strcmp} and so collates
strings byte-by-byte in numerical order.
\item From R-3.2.0: New function \code{trimws()} for removing leading/trailing
\item The \code{lengths} function has been ported from R Core releases
which had NEWS items as below:
Various features from R Core releases.
Added trimws from R-3.2.0.
......@@ -187,3 +187,17 @@ function(x, y, style = c("table", "list"),
# From R-3.2.0:
trimws <-
function(x, which = c("both", "left", "right"))
which <- match.arg(which)
mysub <- function(re, x) sub(re, "", x, perl = TRUE)
if(which == "left")
return(mysub("^[ \t\r\n]+", x))
if(which == "right")
return(mysub("[ \t\r\n]+$", x))
mysub("[ \t\r\n]+$", mysub("^[ \t\r\n]+", x))
\title{Remove Leading/Trailing Whitespace}
Remove leading and/or trailing whitespace from character strings.
trimws(x, which = c("both", "left", "right"))
\item{x}{a character vector}
\item{which}{a character string specifying whether to remove both
leading and trailing whitespace (default), or only leading
(\code{"left"}) or trailing (\code{"right"}). Can be abbreviated.}
For portability, \sQuote{whitespace} is taken as the character class
\code{[ \\t\\r\\n]} (space, horizontal tab, line feed, carriage return).
x <- " Some text. "
trimws(x, "l")
trimws(x, "r")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment