0

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!

7
  • There is already a solution to this problem in your question. What is actually your question? Commented May 22, 2019 at 11:36
  • I think they're asking how to perform the subtraction without converting to an array first Commented May 22, 2019 at 11:45
  • yes. without array i need to give the solutions. Commented May 22, 2019 at 11:55
  • 1
    "i need to give the solutions" – sounds as if you are supposed to find the solution yourself. Commented May 22, 2019 at 12:01
  • 1
    When asked for a clarification of the question, please edit the question rather than elaborating in comments. Questions are intended to stand on their own. That is, readers should not to be expected to read all the comments to understand the question. Commented May 22, 2019 at 14:58

1 Answer 1

3

Use String#tr:

main > 'mangalore'.tr 'mc', '#'
#⇒ "#angalore"
main > 'mc'.tr 'mangalore', '#'
#⇒ "#c"
Sign up to request clarification or add additional context in comments.

4 Comments

Why not delete ?
@steenslag because I have carefully read the OP requirements.
I must be missing something.
@steenslag “[...] for output1 #angalore :) the code above produces the output requested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.