Rajkumar Ulaganadhan
Rajkumar Ulaganadhan

Reputation: 708

How to find and remove the string from string using ruby?

I want to find & remove a string from string. See the examples below,

Input1:

a = 'mangalore'

Input2

b = 'mc'

Outputs

b values should not present in a for output1 #angalore

a values should not present in b for output2 #c

Solutions: converting string a,b to array then doing a-b & b-a

it will give results. How to implement using string in ruby.

Helps appreciated!

Upvotes: 0

Views: 59

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Use String#tr:

main > 'mangalore'.tr 'mc', '#'
#⇒ "#angalore"
main > 'mc'.tr 'mangalore', '#'
#⇒ "#c"

Upvotes: 3

Related Questions