Trims leading and trailing whitespaces from strings or character vectors.

trim(x)

Arguments

x

Character vector or string, or a list or data frame with such vectors. Function is vectorized, i.e. vector may have a length greater than 1. See 'Examples'.

Value

Trimmed x, i.e. with leading and trailing spaces removed.

Examples

trim("white space at end ")
#> [1] "white space at end"
trim(" white space at start and end ")
#> [1] "white space at start and end"
trim(c(" string1 ", "   string2", "string 3   "))
#> [1] "string1"  "string2"  "string 3"

tmp <- data.frame(a = c(" string1 ", "   string2", "string 3   "),
                  b = c(" strong one  ", "    string two", "  third string "),
                  c = c(" str1 ", "   str2", "str3   "))
tmp
#>             a               b       c
#> 1    string1     strong one     str1 
#> 2     string2      string two    str2
#> 3 string 3      third string  str3   
trim(tmp)
#>          a            b    c
#> 1  string1   strong one str1
#> 2  string2   string two str2
#> 3 string 3 third string str3