欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

trimws函数——去掉字符串头尾的空格

程序员文章站 2024-01-20 17:21:34
...

Remove Leading/Trailing Whitespace
作用:去掉字符串头/尾的空格。

  1. Description

Remove leading and/or trailing whitespace from character strings.

  1. Usage

trimws(x, which = c("both", "left", "right"))

  1. Arguments

x
a character vector
which
a character string specifying whether to remove both leading and trailing whitespace (default), or only leading ("left") or trailing ("right"). Can be abbreviated.
Details

For portability, ‘whitespace’ is taken as the character class [ \t\r\n] (space, horizontal tab, line feed, carriage return).

  1. Examples

     x <- "  Some text. "
     x
     trimws(x)
     trimws(x, "l")
     trimws(x, "r")