Avatar of gnanagowthaman sankar
gnanagowthaman sankar
 asked on

please help me to solve the question i cant able to get the logic behind this question

Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive). You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".


withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gnanagowthaman sankar

ASKER
thank you very much it_saige
CEHJ

FYI you can do it in one line with one method of String
it_saige

True, they can, but something tells me that their instructor is looking for a loop based implementation.

-saige-
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
awking00

Take a look at the replaceAll method for strings.
krakatoa

Do you plan on closing any of your other similar open questions, or are you going to ask your way merrily through the entire Codingbat range?
gnanagowthaman sankar

ASKER
Sir how to close the question sir
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krakatoa

ARE you trying to troll us?
gnanagowthaman sankar

ASKER
No am new to this forums honestly don't know how to close it
krakatoa

You were already given a link to that procedure in one of your earlier questions.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
gnanagowthaman sankar

ASKER
Sorry sir please give me the link sir
krakatoa

Err . . .
awking00

Why in the world would codingbat provide such a ridiculous method of looping through a string, comparing characters and length as a solution when the solution is as simple as -
public String withoutString(String str, String remove) {
  return str.removeAll(remove,"");
}
???
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krakatoa

Probably because there is no such method as removeAll. But maybe also because replaceAll doesn't work either.
awking00

krakata,thanks for pointing out my error. Forgive me for the use of removeAll when I meant replaceAll, but that does seem to work. In what case does it not?
krakatoa

Well, you can try it yourself, and satisfy yourself that the code is input as you intended rather than triangulate the matter through me as I may not have understood your intention.
Your help has saved me hundreds of hours of internet surfing.
fblack61
awking00

I did try it before I even posted my comment with the following results:
replaceAll("Hello there","llo") ==> "He there"
replaceAll("Hello there", "e") ==> "Hllo thr"
replaceAll("Hello there", "x") ==> "Hello there"
replaceAll("xxx","xx") ==> "x"
CEHJ

Hint folks: the codingbat problem is case-insensitive
krakatoa

Those are not the results I get. Your code obtains the following afaics :

withoutString results
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krakatoa

Hint folks: the codingbat problem is case-insensitive

CEHJ sir,

Don't quite follow you - - - codingbat supplies the String and the element to remove. I can't see anywhere the cases are exposed to us?

Unless you mean that replaceAll uses regex . . . but that doesn't really help, or does it?
awking00

Thanks, CEHJ. It always helps to read the question carefully. To make the replacement case insensitive, add the case regex insensitive prefix -
public String withoutString(String str, String remove) {
   return str.replaceAll("(?I)" + remove,"");
 }
awking00

Forgive EE's attempts at being helpful "(?I)" was meant to be "(?i)" with a lowercase letter i.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
CEHJ

... was meant to be "(?i)" with a lowercase letter i.
Yes, that was my solution as well ;)