[英]I can't get two String values in one string using templates
I got some function:我得到了一些 function:
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\n")
.shuffled()
.first()
And if I try to get or print some string with the 2 values obtained from this function, the first value disappears.如果我尝试使用从此 function 获得的 2 个值获取或打印一些字符串,第一个值就会消失。 For example:
例如:
println("${selectHometown() ${selectHometown() }")
Will only print one city name, while I expect two.只会打印一个城市名称,而我期望两个。 I guess the problem is related to string concatenation in Kotlin.
我猜这个问题与 Kotlin 中的字符串连接有关。 Of course, I can get the desired result in a different way, but I'm wondering why this one doesn't work.
当然,我可以通过不同的方式获得所需的结果,但我想知道为什么这个不起作用。
Windows way of terminating a line is to use "\r\n" so use it as delimiter: Windows 终止行的方法是使用“\r\n”,因此将其用作分隔符:
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\r\n")
.shuffled()
.first()
println("${selectHometown()} ${selectHometown()}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.