Replace value with nothing in List

Hi Experts,

I have created a list as
val lines = List( “abcd”, “abcde”, “abcdefgh” )
and in this I want to remove a and c values. My O/P should be as below
List(bd,bde,bdefgh)

I have tried with multiple replace. But it is not working
Can anyone explain me how to resolve this issue

Regards,
Kumar

Do a map on the list, and then the map function can be a filter on the string.

Hi,

Can you give me a syntax. Because I am new to programming and scala.
Regards,
Kumar

Use either for/yield:

for (word <- lines) yield {
    for (c <- word if c != 'a' && c != 'c') yield c
}

or map/filter:

lines.map(word => word.filter(c => c != 'a' && c != 'c'))

or a combination of both:

for (word <- lines) yield word.filter(c => c != 'a' && c != 'c')

@kumarleau, I have noticed that the questions you have been posting look like questions that a professor might give to their class. As a professor, I can tell you that if I found a student posting my questions on a public forum to get answers, the penalties for that student would be rather extreme. So I’m hoping that these are just exercises that you found somewhere to work on and not something being used for a class.

Thanks a lot for your help.

1 Like

I had a case last semester in this forum. A student from my class, using an anonymous handler, was asking for answers to his programming assignments. I’ve had it happen in other courses and other forums as well.

Hi Mark,

I have a more than 11+ years experience on BI technologies. Now, I am planning to switch into some big data side technologies. In order to get hands on I am trying to to resolve these kind of scenarios.

Good to hear. If you don’t mind me asking, where are you finding these exercises? They seem to be very Scala focused, but they aren’t ones I have seen online.

One of my close friend has been working on spark - scala for past 2 years. He is asking me to resolve these kind of scenarios. I am going through the scala documentations and blogs to start learning and at the same time to resolve these scenarios. I am feeling very hard to resolve these kind of scenarios. Since I am from different back ground.

That would explain why you get some interesting challenges that seem to vary in their nature.