trim: Trims leading and trailing blanks from a vector of character...

Description Usage Arguments Reuse good code Alternative

Description

Trims leading and trailing blanks from a vector of character variables. Multibyte character strings are returned intact. (extended ascii in R appears as hex values).

Usage

1
trim(s)

Arguments

s

Character vector.

Reuse good code

"The single biggest way to improve both the quality of your code and your productivity is to reuse good code (...) Many algorithms have already been invented, tested, discussed in the trade literature, reviewed, and improved".

–Code complete 2, by Steve McConnell.

Alternative

A more powerful and general alternative is stringr::str_trim():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
library(ctfs)
library(stringr)

string <-  " hello   "

# You can do the same.
trim(string)
#> [1] "hello"
str_trim(string)
#> [1] "hello"

# And more.
str_trim(string, "right")
#> [1] " hello"

forestgeo/ctfs documentation built on May 3, 2019, 6:44 p.m.