To remove all whitespace characters from a string in R, you can use the `gsub` function with a regular expression pattern.
In this example,
str
with a value of "Hello World"
.gsub
function with a regular expression pattern "\\s"
to replace all whitespace characters (including tabs, spaces, newlines, etc.) with an empty string in str
.str
.str <- 'Hello World'
modifiedStr <- gsub('[[:space:]]', '', str)
print(paste('Modified string:', modifiedStr))
[1] "Modified string: HelloWorld"
In this example,
str
with a value of "apple\tbanana\n cherry "
.gsub
function with a regular expression pattern "\\s"
to replace all whitespace characters (including tabs, spaces, newlines, etc.) with an empty string in str
.str
.str <- 'apple\tbanana\n cherry '
modifiedStr <- gsub('[[:space:]]', '', str)
print(paste('Modified string:', modifiedStr))
[1] "Modified string: applebananacherry"
In this tutorial, we learned How to Remove All Whitespace Characters from a String in R language with well detailed examples.