我是编程新手.最近,我开始学习Kotlin.
var cont = "N"
var result:Int?
result = 45
println (cont + " + " + result)
It suggests converting this {" + "}
to a template, but I do not know how?
我是编程新手.最近,我开始学习Kotlin.
var cont = "N"
var result:Int?
result = 45
println (cont + " + " + result)
It suggests converting this {" + "}
to a template, but I do not know how?
在Kotlin中,可以使用字符串模板删除代码中的所有连接符号.他们总是从$
开始.
例如,在您的代码中,您可以这样做:
println("$cont + $result")
This would print the same result as your original code, just more concise and readable. This can even be done on arbitrary expressions you just have to wrap it in curly braces.
例如:
val cont = "ALEC"
println("Hi ${cont.toLowerCase()}") //prints Hi alec
As mentioned in the comments, IntelliJ will do this automagically by hitting ALT + Enter
when the hint is suggested.