Remove White Spaces from a String

str_trim() is opposite to str_pad(), and removes the white spaces from a string. By default, white spaces at both the left and right side of the string will be removed. Internal spaces, however, are not touched.

library(stringr)
x <- c(" french fries, ", " BBQ chicken, ", "and lemonade. ")
str_trim(x, side = "both")

Output:

[1] "french fries," "BBQ chicken," "and lemonade."

str_squish() not only removes white spaces at the two sides of a string, but also reduces internal consecutive white spaces into a single white space.

str_squish(x)

Output:

[1] "french fries," "BBQ chicken," "and lemonade."