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
whitespace.
\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"),
}
r
}
# 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))
}
\name{trimws}
\alias{trimws}
\title{Remove Leading/Trailing Whitespace}
\description{
Remove leading and/or trailing whitespace from character strings.
}
\usage{
trimws(x, which = c("both", "left", "right"))
}
\arguments{
\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.}
}
\details{
For portability, \sQuote{whitespace} is taken as the character class
\code{[ \\t\\r\\n]} (space, horizontal tab, line feed, carriage return).
}
\examples{
x <- " Some text. "
x
trimws(x)
trimws(x, "l")
trimws(x, "r")
}
\keyword{character}
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