• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing How to Remove All Whitespace from a String in R?

To remove all whitespace from a string in R, you can use the base R function gsub() and functions from third-party libraries of R like stringr and stringi. In this article, I will explain how to remove all whitespace from a string using the following methods with multiple examples.

Advertisements
  • gsub() function from R base
  • str_replace_all() function from the stringr package
  • str_trim() function from the stringr package
  • stri_replace_all_charclass() from the stringi package

1. Quick Examples of Removing All Whitespace from a String in R

If you are in a hurry, below are quick examples of removing all whitespace from a string.


# Below are the quick examples of removing all white sapce

# Initialize the string 
string = "Welcome to Spark By Examples"

# Example 1: Remove all white spaces using R base gsub()
print(gsub(" ", "", string)) 

# Example 2: Remove all white space from a string 
# using gsub() by [[:blank:]]
print(gsub("[[:blank:]]", "", string))

# Example 3: Remove all white space using str_replace_all()
library(stringr)
print(str_replace_all(string, " ", "")) 

# Example 4: Remove all white space using str_replace_all()
# by \\s
print(str_replace_all(string, regex("\\s*"), ""))

# Example 5:  Remove all white space from left of string
# using str_trim()
string = "  Welcome to Spark By Examples"
print(str_trim(string, "left"))

# Example 6: Remove all white space from right of string
# using str_trim()
string = "Welcome to Spark By Examples   "
print(str_trim(string, "right"))

# Example 7: Remove all white space both sides of string
# using str_trim()
string = "   Welcome to Spark By Examples   "
print(str_trim(string, "both"))

# Example 8: Remove all white space both sides of string
# using stri_replace_all_charclass()
library(stringi)
string = "Welcome to Spark By Examples"
print(stri_replace_all_charclass(string, "\\p{WHITE_SPACE}", ""))

2. Remove All Whitespace from a String Using the gsub()

The gsub() is the base R function which is used to replace all occurrences of a specified pattern within a string in R programming language. It takes the string along with specified pattern and replacement as arguments and returns the string with the desired replacement.

2.1 Syntax of gsub() function

Following is the syntax of the gsub() function. The gsub() function takes three main arguments: the pattern to be replaced, the replacement value, and the character vector or string where the substitutions should occur.


# Syntax of gsub() function
gsub(<pattern>, <replacement>, <input_string>)  

2.2 Parameters

  • pattern: It takes the pattern to be replaced.
  • replacement: It replaces the specified text pattern within a string.
  • input_string: The input string

2.3 Replace White spaces from String using gsub() function

You can use the R gsub() function to remove all white spaces from a String. The gsub() is a function in R that stands for “global substitution” and is used for replacing all occurrences of a pattern within a character vector or string. This function is particularly useful for text manipulation tasks, allowing users to find and replace specific patterns in strings.


# Initialize the string 
string = "Welcome to Spark By Examples"
print("Original String:") 
print(string) 

print("After removing white space from a given string:") 

# Remove all white spaces using R base gsub()
print(gsub(" ", "", string)) 
 

In this example, it replaces all occurrences of the space character (" ") with an empty string (""). The result is then printed to the console.

remove all whitespace from a string in r

Alternatively, you can use the gsub() function in another way. In the below example, you can represent the white space with [[:blank:]] and pass it into the gsub() function along with replacement("") argument. It will replace all occurrences of whitespace with an empty string.


# Remove all white space from a string by [[:blank:]]
string = "Welcome to Spark By Examples"
print(gsub("[[:blank:]]", "", string))

# Output:
# [1] "WelcometoSparkByExamples"

3. Using str_replace_all() function

The str_replace_allI() function is available in the stringr package, which provides functions for working with strings. To install this package, use the install.packages() function in the console pane at the bottom of Rstudio. After installing the package you need to load the package into your R session using the library(stringr) function.

This function takes in the input string, pattern, and replacement as arguments and returns the original string with a specified replacement of the given pattern.


# Remove all white space using str_replace_all()
library(stringr)

# Initialize the string 
string = "Welcome to Spark By Examples"
print("Original String:") 
print(string) 
print("After removing white space from given a string:") 
print(str_replace_all(string, " ", "")) 

Yields the same as the above output. Here, the syntax str_replace_all(string, " ", "") replaces all occurrences of a space (” “) with an empty string (“”). This effectively removes all white spaces from the original string.

Similarly, you can use the str_replace_all() function in another way to remove all white spaces from a given string. Here, you can represent the white space with a regular expression(\\s*) patterns.


# Remove all white space using str_replace_all()
# by \\s
# Load stringr package
library(stringr)
string = "Welcome to Spark By Examples"
print(str_replace_all(string, regex("\\s*"), ""))
and

Yields the same as the above output.

4 Remove Beginning & Ending All Whitespaces Using str_trim()

The str_trim function is a part of stringr package that removes only the starting and ending all whitespace of a given string. You can also replace all whitespace on both sides using this function.

Let’s use str_trim(string, "left"), this syntax removes all whitespace on the left side of a string with an empty string.


# Remove all white space from left of string
# using str_trim()
library(stringr)
string = "   Welcome to Spark By Examples"
print(str_trim(string, "left"))

# Output:
# [1] "Welcome to Spark By Examples"

Use str_trim(string, "right") to remove all whitespace on the right side of a string with an empty string.


# Remove all white space from right of string
# using str_trim()
library(stringr)
string = "Welcome to Spark By Examples   "
print(str_trim(string, "right"))

# Output:
# [1] "Welcome to Spark By Examples"

Use str_trim(string, "both") to remove all whitespace of both sides of a string with an empty string.


# Remove all white space both sides of string
# using str_trim()
library(stringr)
string = "   Welcome to Spark By Examples   "
print(str_trim(string, "both"))

# Output:
# [1] "Welcome to Spark By Examples"

5. Using stri_replace_all_charclass() Function

The stri_replace_all_charclass() function from the stringi package also removes all white spaces from the given string.


# Remove all white space both sides of string
# using stri_replace_all_charclass()
library(stringi)
string = "Welcome to Spark By Examples"
print(stri_replace_all_charclass(string, "\\p{WHITE_SPACE}", ""))

# Output:
# [1] "Welcome to Spark By Examples"

Here,

stri_replace_all_charclass(string, "\\p{WHITE_SPACE}", "") replaces all characters belonging to the Unicode property “WHITE_SPACE” with an empty string in a given string. where \\p{WHITE_SPACE} is a regular expression pattern specifying the Unicode property for white space characters.

6. Conclusion

In this article, I have explained how to remove all whitespace from a string using the base R gsub()function, and str_replace_all(), str_trim(), and stri_replace_all_charclass() functions of third-party libraries of R language like stringr and stringi packages.

Happy learning!!

Related Article