Kotlin Remove all non alphanumeric characters

KotlinKotlin Android-Extensions

Kotlin Problem Overview


I am trying to remove all non alphanumeric characters from a string.

I tried using replace() with a regex as followed:

var answer = answerEditText.text.toString()
Log.d("debug", answer)
answer = answer.replace("[^A-Za-z0-9 ]", "").toLowerCase()
Log.d("debug", answer)

>D/debug: Test. ,replace

>D/debug: test. ,replace

Why are the punctuation characters still present? How to get only the alphanumeric characters?

Kotlin Solutions


Solution 1 - Kotlin

You need to create a regex object

var answer = "Test. ,replace"
println(answer)
answer = answer.replace("[^A-Za-z0-9 ]", "") // doesn't work
println(answer)
val re = Regex("[^A-Za-z0-9 ]")
answer = re.replace(answer, "") // works
println(answer)

Try it online: https://try.kotlinlang.org/#/UserProjects/ttqm0r6lisi743f2dltveid1u9/2olerk6jvb10l03q6bkk1lapjn

Solution 2 - Kotlin

The standard library of Kotlin is beautiful like this. Just use String.filter combined with Char.isLetterOrDigit, like this:

val stringToFilter = "A1.2-b3_4C"
val stringWithOnlyDigits = stringToFilter.filter { it.isLetterOrDigit() }
println(stringWithOnlyDigits) //Prints out "A12b34C"

Solution 3 - Kotlin

I find this to be much more succinct and maintainable. Could be that the previous answers were made before these extensions were added?

val alphaNumericString = someString.toCharArray()
   .filter { it.isLetterOrDigit() }
   .joinToString(separator = "")

Solution 4 - Kotlin

You need to create a regex, this can be done by str.toRegex() before calling replace

val string = "AsAABa3a35e8tfyz.a6ax7xe"
string = string.replace(("[^\\d.]").toRegex(), "")

result: 3358.67

In case you need to handle words W and spaces

var string = "Test in@@ #Kot$#lin   FaaFS@@#$%^&StraßeFe.__525448=="
    string = string.replace(("[^\\w\\d ]").toRegex(), "")
    println(string)

result: Test in Kotlin FaaFSStraeFe__525448

Solution 5 - Kotlin

I think it's easiest way:

fun String.toNumericString() = this.filter { it.isDigit() }

Solution 6 - Kotlin

fun String.digitsOnly(): String{
    val regex = Regex("[^0-9]")
    return regex.replace(this, "")
}
fun String.alphaNumericOnly(): String{
    val regex = Regex("[^A-Za-z0-9 ]")
    return regex.replace(this, "")
}

Usage:

val alphaNumeric = "my string #$".alphaNumericOnly()

Solution 7 - Kotlin

Kotlin thinks you substituting string, but not regex, so you should help a little bit to choose right method signature with regex as a first argument.

Use Regex type explicitly instead of string:

"[^A-Za-z0-9 ]".toRegex()

or tell that you are passing named regex parameter:

answer.replace(regex = "[^A-Za-z0-9 ]", "")

and in this case kotlin wont compile unless you pass real regex, not string

Solution 8 - Kotlin

You can try without regex, for example:

val ranges = ('0'..'9') + ('a'..'z') + ('A'..'Z')
val escaped = "1! at __ 2? at 345..0986 ZOk".filter { it in ranges }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDistwoView Question on Stackoverflow
Solution 1 - KotlinhasenView Answer on Stackoverflow
Solution 2 - KotlinMad Scientist MosesView Answer on Stackoverflow
Solution 3 - KotlinKyle LuceView Answer on Stackoverflow
Solution 4 - KotlinFaakhirView Answer on Stackoverflow
Solution 5 - KotlinArtem BotnevView Answer on Stackoverflow
Solution 6 - KotlinBitcoin Cash - ADA enthusiastView Answer on Stackoverflow
Solution 7 - KotlinMaksim KostrominView Answer on Stackoverflow
Solution 8 - KotlinSergey MotyrevView Answer on Stackoverflow