user3022875
user3022875

Reputation: 9018

Replace a character in a text

How do I replace the + with %2B:

Here is my code

x<-"asflj + ldjjsf ljsdlafj"
gsub("+","%2B",  x)

my output is:

"%2Ba%2Bs%2Bf%2Bl%2Bj%2B %2B+%2B %2Bl%2Bd%2Bj%2Bj%2Bs%2Bf%2B %2Bl%2Bj%2Bs%2Bd%2Bl%2Ba%2Bf%2Bj%2B"

and I want it to be "asflj %20 ldjjsf ljsdlafj"

Upvotes: 1

Views: 157

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

+ is a metacharacter then you can set fixed=TRUE to skip it and get what you need.

> gsub("+","%2B",  x, fixed=TRUE)
[1] "asflj %2B ldjjsf ljsdlafj"

Or skip it by using \\+

> gsub("\\+","%2B",  x)
[1] "asflj %2B ldjjsf ljsdlafj"

Upvotes: 3

Related Questions