trimws()
trimws(x, which = c("both", "left", "right"), whitespace = "[[:space:]]") character · Added in v3.2.0 · Updated March 13, 2026 · String Functions The trimws() function in R removes leading and/or trailing whitespace from character strings. It’s part of base R, making it available without any additional packages. This function is essential for cleaning text data, particularly when working with user input, file imports, or any scenario where unwanted spaces might cause issues with string matching or processing.
Syntax
trimws(x, which = c("both", "left", "right"), whitespace = "[[:space:]]")
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | character vector | Required | A character vector whose strings should be trimmed |
which | string | "both" | Which side to trim: "both", "left" (or "l"), or "right" (or "r") |
whitespace | string | "[[:space:]]" | Regular expression pattern matching whitespace characters |
Return Value
Returns a character vector of the same length as x, with leading and/or trailing whitespace removed according to the which parameter. NA values in x are preserved as NA.
Examples
Basic Usage
# Trim whitespace from both ends
x <- " some text "
trimws(x)
# [1] "some text"
# Trim only from the left
trimws(x, "left")
# [1] "some text "
# Trim only from the right
trimws(x, "right")
# [1] " some text"
# Using shorthand - "l" and "r" work too
trimws(x, "l")
# [1] "some text "
Working with Character Vectors
# trimws() is vectorized - processes each element
words <- c(" apple", "banana ", " cherry ", "date")
trimws(words)
# [1] "apple" "banana" "cherry" "date"
# Useful after reading data with extra spaces
dirty_data <- c(" 2024-01-01 ", " 2024-01-02 ", "2024-01-03")
trimws(dirty_data)
# [1] "2024-01-01" "2024-01-02" "2024-01-03"
Handling Different Types of Whitespace
# Default handles regular spaces, tabs, newlines
messy <- "\t text with tabs and newlines\n "
trimws(messy)
# [1] "text with tabs and newlines"
# Use whitespace parameter for custom patterns (e.g., non-breaking spaces)
text_with_nbsp <- paste0("\u00a0", "clean me", "\u00a0") # non-breaking space
trimws(text_with_nbsp) # default doesn't remove nbsp
# [1] "\u00a0clean me\u00a0"
# Custom pattern to handle non-breaking spaces
trimws(text_with_nbsp, whitespace = "[\\h\\v]")
# [1] "clean me"
Practical Applications
# Cleaning user input for comparison
user_input <- " john@example.com "
db_email <- "john@example.com"
# Without trimming - comparison fails
user_input == db_email
# [1] FALSE
# With trimming - comparison succeeds
trimws(user_input) == db_email
# [1] TRUE
# Preparing data for analysis
raw_names <- c(" Alice ", "Bob ", " Charlie")
trimws(raw_names)
# [1] "Alice" "Bob" "Charlie"
# Combining with other string functions
full_name <- " john doe "
gsub(" +", " ", trimws(full_name)) # collapse multiple spaces
# [1] "john doe"
Handling NA Values
# NA values are preserved
text_with_na <- c(" hello ", NA, " world ", NA)
trimws(text_with_na)
# [1] "hello" NA "world" NA
# Use with other NA-handling functions
text <- c(" text1 ", NA, " text2 ")
trimws(text)
# [1] "text1" NA "text2"
Common Patterns
-
Data Import Cleaning: After reading CSV or text files, use
trimws()on character columns to remove accidental leading/trailing spaces that can break joins or comparisons. -
Form Input Validation: Trim user form submissions before validation or storage to ensure consistent data.
-
Text Normalization: Combine
trimws()withtolower()orgsub()for consistent text normalization before analysis. -
String Matching: Always trim before comparing strings to avoid false negatives from invisible whitespace.