Dynamically evaluating templated Strings in Kotlin

后端 未结 2 1231
轻奢々
轻奢々 2021-01-03 10:41

Suppose that I have the following piece of Kotlin code:

fun main(args: Array) {
    val a = \"test\"
    println(args.first())
}
2条回答
  •  被撕碎了的回忆
    2021-01-03 11:06

    Is there some way to evaluate Strings that aren't in the source code with regards to templates in their current context?

    There are no template strings in your code sample and a is unused. Am I understanding correctly that you'd like to do something like val evaluated = evalStringTemplate(template, arg1, arg2, ...) with template being a String like "$a" and arg1, arg2, ... being arguments for the template?

    If so, there's no Kotlin-specific way to do this, but you can use the Java Formatter class.

提交回复
热议问题