The R Project SVN R

Rev

Rev 67678 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 67678 Rev 74838
Line 3... Line 3...
3
\title{Remove Leading/Trailing Whitespace}
3
\title{Remove Leading/Trailing Whitespace}
4
\description{
4
\description{
5
  Remove leading and/or trailing whitespace from character strings.
5
  Remove leading and/or trailing whitespace from character strings.
6
}
6
}
7
\usage{
7
\usage{
8
trimws(x, which = c("both", "left", "right"))
8
trimws(x, which = c("both", "left", "right"), whitespace = "[ \\t\\r\\n]")
9
}
9
}
10
\arguments{
10
\arguments{
11
  \item{x}{a character vector}
11
  \item{x}{a character vector}
12
  \item{which}{a character string specifying whether to remove both
12
  \item{which}{a character string specifying whether to remove both
13
    leading and trailing whitespace (default), or only leading
13
    leading and trailing whitespace (default), or only leading
14
    (\code{"left"}) or trailing (\code{"right"}).  Can be abbreviated.}
14
    (\code{"left"}) or trailing (\code{"right"}).  Can be abbreviated.}
-
 
15
  \item{whitespace}{a string specifying a regular expression to match
-
 
16
    (one character of) \dQuote{white space}, see Details for
-
 
17
    alternatives to the default.}
15
}
18
}
16
\details{
19
\details{
-
 
20
  Internally, \code{\link{sub}(re, "", *, perl = TRUE)}, i.e., PCRE
-
 
21
  library regular expressions are used.
17
  For portability, \sQuote{whitespace} is taken as the character class
22
  For portability, the default \sQuote{whitespace} is the character class
18
  \code{[ \\t\\r\\n]} (space, horizontal tab, line feed, carriage return).
23
  \code{[ \\t\\r\\n]} (space, horizontal tab, carriage return,
-
 
24
  newline).  Alternatively, \code{[\\h\\v]} is a good (PCRE)
-
 
25
  generalization to match all Unicode horizontal and vertical white
-
 
26
  space characters, see also \url{https://www.pcre.org}.
19
}
27
}
20
\examples{
28
\examples{
21
x <- "  Some text. "
29
x <- "  Some text. "
22
x
30
x
23
trimws(x)
31
trimws(x)
24
trimws(x, "l")
32
trimws(x, "l")
25
trimws(x, "r")
33
trimws(x, "r")
-
 
34
 
-
 
35
## Unicode --> need "stronger" 'whitespace' to match all :
-
 
36
tt <- "text with unicode 'non breakable space'."
-
 
37
xu <- paste(" \\t\\v", tt, "\\u00a0 \\n\\r")
-
 
38
(tu <- trimws(xu, whitespace = "[\\\\h\\\\v]"))
-
 
39
stopifnot(identical(tu, tt))
26
}
40
}
27
\keyword{character}
41
\keyword{character}