How to replace a space by semicolon between two words with gsub()

Asked

Viewed 737 times

2

2 answers

6


Here it comes:

ex <- "[email protected] [email protected]"

# Com o gsub()
gsub(pattern = "[[:blank:]]",
     replacement = ";",
     x =  ex)

# Com str_replace_all(), que eu prefiro
library(stringr)
str_replace_all(string = ex,
                pattern = "[[:blank:]]",
                replacement = ";")

You could do that too:

str_replace_all(string = ex,
                pattern = "[ ]",
                replacement = ";")

Or else this:

str_replace_all(string = ex,
                pattern = "[[:space:] ]",
                replacement = ";")

[[:blank:]] works to eliminate all types of white space, including tabs, etc. With [ ] (a gap between brackets) you only remove the same common space. And with [[:space:]] it is possible to delete spaces, tabs and also line breaks.

  • i was writing the [[:Blank:]] wrong and it wasn’t working. Thank you!

-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.