从字符串中删除前导和/或尾随空格。
trimws(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]")
x : 字符向量
which : 一个字符串,指定是同时删除前导空格和尾随空格(默认值),还是只删除前导空格(“左”)或尾随空格(“右”)。可以缩写。
whitespace : 指定要匹配的正则表达式的字符串(空格的一个字符),请参阅默认值的替代项的详细信息。
x < - " Some text. "
x
trimws(x)
trimws(x, "l")
trimws(x, "r")
## Unicode --> need "stronger" 'whitespace' to match all :
tt < - "text with unicode 'non breakable space'."
xu < - paste(" \t\v", tt, "\u00a0 \n\r")
(tu < - trimws(xu, whitespace = "[\\h\\v]"))
stopifnot(identical(tu, tt))