简体   繁体   中英

I can't get two String values in one string using templates

I got some 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. 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. 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:

private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\r\n")
.shuffled()
.first()

println("${selectHometown()} ${selectHometown()}")

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