0

I got a string;

"Enviroment is dangerous.123"

Now I want to remove everything after "dangerous" so the result will be

"Enviroment is dangerous"

I got different text strings of different length. So it needs to respond to the string "dangerous"

How do I do that?

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

2

We can use sub to match the . followed by one or more numbers (\\d+) until the end of the string ($) and replace with blank ("")

sub("\\.\\d+$", "", str1)
#[1] "Enviroment is dangerous"

data

str1 <-  "Enviroment is dangerous.123"
akrun
  • 874,273
  • 37
  • 540
  • 662