Skip to contents

The function cleanup_spaces() takes a character vector and input and turns any uninterrupted stretch of whitespace characters into one single space character. Moreover, it can also remove leading whitespace and trailing whitespace.

Usage

cleanup_spaces(x, remove_leading = TRUE, remove_trailing = TRUE)

Arguments

x

Character vector.

remove_leading

Logical. If TRUE, leading whitespace will be removed.

remove_trailing

Logical. If TRUE, trailing whitespace will be removed.

Value

A character vector.

Examples

txt <- "  A \\t  small      example \\n with redundant whitespace    "
cleanup_spaces(txt)
#> [1] "A \\t small example \\n with redundant whitespace"
cleanup_spaces(txt, remove_leading = FALSE, remove_trailing = FALSE)
#> [1] " A \\t small example \\n with redundant whitespace "