remove_space: Remove and normalize spaces

View source: R/utils.R

remove_spaceR Documentation

Remove and normalize spaces

Description

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)
remove_space(x, trim_start = FALSE)
remove_space(x, trim_end = TRUE)
remove_space(x, collapse_multiple = FALSE)
remove_space(
  x,
  trim_start = FALSE,
  trim_end = FALSE,
  collapse_multiple = FALSE
)

# with newlines
multiline <- c(
  "hello\n\n  world  ",
  "  first  \n  second  "
)
remove_space(multiline)
remove_space(multiline, preserve_newlines = FALSE)

thisutils documentation built on July 3, 2025, 9:09 a.m.