简体   繁体   English

我无法使用模板在一个字符串中获取两个字符串值

[英]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.

相关问题 如何在两个字符之间获取字符串[string]? PHP - How can I get string between two characters [string] ? PHP 从两个字符串值之间获取一个字符串 - Get a string from between two string values 如何获得以两个字符之一结尾的字符串,并用regexp有条件地替换最后一个字符? - How can I get a string that finishes with one of two characters and replace that last character conditionally by regexp? Python 无法根据两个整数的值为字符串赋值 - Python can't assign value to a string based on values of two integers 将字符串数组从活动1传递到活动2,从字符串数组获取值并相应地更改布局 - pass string array from activity one to activity two, get values from string array and change layout accordingly 如何使用C#使用字典将位于一个字符串列表中的键映射到位于另一个字符串列表中的值? - How can I use dictionary to map keys located in one string list to values located in another string list using C#? 我怎样才能使一个字符串基于其他两个字符串? - How can I make one string based on others two? 无法获取字符串数组来复制一个随机值 - Can't get a string array to copy one random value 使用string.next在循环内分配哈希值。 我无法解释以下输出 - Using string.next to assign hash values inside a loop. I can't explain the following output 当$ STRING以“\\ t \\ t”结尾时,如何使split(/ \\ t /,$ STRING)检测空值? - How can I make split(/\t/, $STRING) detect empty values when $STRING ends with “\t\t”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM