Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove characters from a string that occur in another string in Kotlin

Let me preface this by saying I am really new to Kotlin but am a little familiar with Python.

My goal is to remove all the occurrences of the characters in one string from another string via some sort of function.

I can show you how I would do this in Python :

def removechars (s, chars)
    return s.translate(None, chars)

And I can use it like this :

print(removechars("The quick brown fox jumped over the sleazy dog!", "qot"))

It would give this output :

The uick brwn fx jumped ver the sleazy dg!     

How can I something similar in Kotlin?

like image 340
Patrick Star Avatar asked Feb 04 '23 06:02

Patrick Star


1 Answers

I suggest using filterNot() in Kotlin:

"Mississippi".filterNot { c -> "is".contains(c)}

This should output "Mpp".

like image 162
Jalvap Avatar answered Feb 07 '23 07:02

Jalvap