0

For my case, it's an web strings, but here I will just give an easier example:`

df = data.frame(Strings = c("abc/d/e/f////", "abc///", "/", "a/bc/d/////"))

The result that I want to gain is make the string which is ended with multiple "/" into there is only one "/" left at the end of the strings.

Which means that for the df I gave above, I want to get a result like this:

df_result = data.frame(Strings = c("abc/d/e/f/", "abc/", "/", "a/bc/d/"))

Thanks for answering my question.

Denny Chen
  • 489
  • 3
  • 8
  • 3
    Could you remove the pictures of code and simply copy and paste into the question? – NelsonGon Mar 16 '19 at 13:48
  • 2
    Hi and welcome to Stack Overflow! To help you effectively, we need all the necessary information to reproduce your problem, i.e. *working* code and example data. There are several [*ways to provide data*](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), probably adding the output of `dput(head())` to your question is sufficient. Avoid providing code as an image! Consider [*How to make a great reproducible example*](https://stackoverflow.com/help/mcve) and edit your question accordingly, cheers! – jay.sf Mar 16 '19 at 13:49
  • oh Sorry for my question, this is my first time to ask questions here. I apologize for my ignorance. – Denny Chen Mar 17 '19 at 05:20

1 Answers1

1

We could use:

string1<-"abc/d/e/f///"
gsub("/(?=/{1,})","",string1,perl=TRUE)
#[1] "abc/d/e/f/"
NelsonGon
  • 13,015
  • 7
  • 27
  • 57