简体   繁体   中英

How do I replace letters with numbers using the replace function in kotlin inside a lambda expression

mood = "leet"
modifier = { message ->
    val regex = """(L|e|t)""".toRegex() 
    //Clueless about what to do after this         
}

THIS IS WHAT I CAME UP WITH SO FAR, THE QUESTION IN THE BOOK BIG NERD RANCH KOTLIN EDITION 2 SAYS "leet (or 1337): The narrator will speak in leetspeak, replacing letters with numbers and symbols that look similar. For example, 'L' becomes '1'; 'E' becomes '3'; 'T' becomes '7'. (Hint: Take a look at String's replace function. There is a version that accepts a lambda as the second parameter.)"

This is the function they're telling you to look at, specifically this one:

inline fun CharSequence.replace(
    regex: Regex,
    noinline transform: (MatchResult) -> CharSequence
): String

Returns a new string obtained by replacing each substring of this char sequence that matches the given regular expression with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

So the lambda you provide is a function that takes a MatchResult and does something with it, and returns a CharSequence (which can be a one-character long String ). The replace function calls that lambda for every match that regex makes.

You get the general idea of what you're supposed to do? You have two parts here - the thing that identifies parts of the input string to process, and the thing that takes those matches and changes them into something else. The result is the original string with those changes made. So you need to come up with a regex and a transform that work together.

Nobody (probably) is going to tell you the answer because the point is figuring it out for yourself, but if you have any questions about things like regexes people will be happy to help you out, And speaking of: this site is extremely useful (I just used it myself to check I knew what I was doing): https://regex101.com/

Here is the implementation as pointed by @cactustictacs:

5 -> {
        mood = "leet"
        val regex: Regex = """[LET]""".toRegex()
        modifier = { message ->
            message.uppercase().replace(regex) { m ->
                when (m.value) {
                    "L" -> "1"
                    "E" -> "3"
                    "T" -> "7"
                    else -> ""
                }
            }
        }
    }

and here is the another method almost same but with minor change using regex.replace()

5 -> {
        mood = "leet"
        val regex: Regex = """[LET]""".toRegex()
        modifier = { message ->
            regex.replace(message.uppercase()){m ->
                when (m.value) {
                    "L" -> "1"
                    "E" -> "3"
                    "T" -> "7"
                    else -> ""
                }
            }
        }
    }

You can use it in place of m to make it slightly more concise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM