Skip to contents

Remove leading/trailing spaces and normalize multiple spaces between words in character strings.

Usage

remove_space(
  x,
  trim_start = TRUE,
  trim_end = FALSE,
  collapse_multiple = TRUE,
  preserve_newlines = TRUE
)

Arguments

x

A vector of character strings.

trim_start

Logical value, default is `TRUE`. Whether to remove leading spaces before the first word.

trim_end

Logical value, default is `FALSE`. Whether to remove trailing spaces after the last word.

collapse_multiple

Logical value, default is `TRUE`. Whether to collapse multiple consecutive spaces between words into a single space.

preserve_newlines

Logical value, default is `TRUE`. Whether to preserve newline characters when collapsing spaces.

Value

A character vector with spaces normalized according to the specified parameters.

Examples

x <- c(
  " hello  world ",
  "  test   case  ",
  "no space",
  "   multiple   spaces   "
)
remove_space(x)
#> [1] "hello world "     "test case "       "no space"         "multiple spaces "
remove_space(x, trim_start = FALSE)
#> [1] " hello world "     " test case "       "no space"         
#> [4] " multiple spaces "
remove_space(x, trim_end = TRUE)
#> [1] "hello world"     "test case"       "no space"        "multiple spaces"
remove_space(x, collapse_multiple = FALSE)
#> [1] "hello  world "        "test   case  "        "no space"            
#> [4] "multiple   spaces   "
remove_space(
  x,
  trim_start = FALSE,
  trim_end = FALSE,
  collapse_multiple = FALSE
)
#> [1] " hello  world "          "  test   case  "        
#> [3] "no space"                "   multiple   spaces   "

# with newlines
multiline <- c(
  "hello\n\n  world  ",
  "  first  \n  second  "
)
remove_space(multiline)
#> [1] "hello\nworld "  "first\nsecond "
remove_space(multiline, preserve_newlines = FALSE)
#> [1] "hello world "  "first second "