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

74 Views Asked by At

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.

1

There are 1 best solutions below

2
Ilham On

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()}")