Guru S
Guru S

Reputation: 7

How do i remove string if and only if end of string using r programming

I want to remove Bangalore from list of strings and Bangalore should be end of the string in the list.

Input data:

TCS bangalore
Wipro global Bangalore
Bangalore consultant India private limited
Infosys Bangalore
systems bangalore pvt ltd

Expected Output data:

TCS
Wipro global
Bangalore consultant India private limited
Infosys
systems bangalore pvt ltd

Help me to get expected output in R programming.

Upvotes: 0

Views: 60

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521083

Use sub in case insensitive mode:

output <- sub("\\s+Bangalore$", "", input, ignore.case=TRUE)
output

[1] "TCS"                                       
[2] "Wipro global"                              
[3] "Bangalore consultant India private limited"
[4] "Infosys"                                   
[5] "systems bangalore pvt ltd"                 

Data:

input <- c("TCS bangalore",
           "Wipro global Bangalore",
           "Bangalore consultant India private limited",
           "Infosys Bangalore",
           "systems bangalore pvt ltd")

Upvotes: 3

Related Questions