Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to remove specific characters from a string? (kotlin)

So I have a textField where you should enter your "coded" text and get it translated back to non-coded language by using .replace to remove certain characters. But I can't get it to work.

There is a kids "code-language" where you take a word, like cat and for every consonant you add an "o" and the consonant again. So a "b" would be "bob". With vowels they just stay as they are. Cat would be cocatot.

fun translateBack(view : View) {
     val konsonanter = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"
     var input = editText.text.toString()
     var emptyString = ""

     for(i in konsonanter) {
        val find_text = i + "o" + i

        var conso = i.toString()

        textView.text = input.replace(find_text, conso, false)
     }
 }

Would like for it to remove the two following letters for every consonant (if possible). So if i enter "cocowow" I should get out "cow". Right now I just get back what I enter in the textField...

like image 398
Henrik Avatar asked Jan 21 '26 22:01

Henrik


1 Answers

Use a forEach loop through the chars of input and replace:

konsonanter.forEach { input = input.replace("${it}o${it}", "${it}", false) }
textView.text = input
like image 82
forpas Avatar answered Jan 24 '26 16:01

forpas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!