string Kotlin - 如何正确连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44188240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kotlin - How to correctly concatenate a String
提问by Daniele
A very basic question, what is the right way to concatenate a String in Kotlin?
一个非常基本的问题,在 Kotlin 中连接字符串的正确方法是什么?
In Java you would use the concat()
method, e.g.
在 Java 中,您将使用该concat()
方法,例如
String a = "Hello ";
String b = a.concat("World"); // b = Hello World
The concat()
function isn't available for Kotlin though. Should I use the +
sign?
但是,该concat()
功能不适用于 Kotlin。我应该使用这个+
标志吗?
回答by Avijit Karmakar
In Kotlin, you can concatenate using string interpolation / templates:
在 Kotlin 中,您可以使用字符串插值/模板进行连接:
val a = "Hello"
val b = "World"
val c = "$a $b"
The output will be: Hello World
输出将是: Hello World
Or you can concatenate using the +
/ plus()
operator:
或者您可以使用+
/plus()
运算符进行连接:
val a = "Hello"
val b = "World"
val c = a + b // same as calling operator function a.plus(b)
print(c)
The output will be: HelloWorld
输出将是: HelloWorld
Or you can concatenate using the StringBuilder
.
或者您可以使用StringBuilder
.
val a = "Hello"
val b = "World"
val sb = StringBuilder()
sb.append(a).append(b)
val c = sb.toString()
print(c)
The output will be: HelloWorld
输出将是: HelloWorld
回答by Harald Gliebe
kotlin.String
has a plus
method:
kotlin.String
有一个plus
方法:
a.plus(b)
See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/plus.htmlfor details.
有关详细信息,请参阅https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/plus.html。
回答by Tushar
Yes, you can concatenate using a +
sign. Kotlin has string templates, so it's better to use them like:
是的,您可以使用+
符号进行连接。Kotlin 有字符串模板,所以最好像这样使用它们:
var fn = "Hello"
var ln = "World"
"$fn $ln"
for concatenation.
"$fn $ln"
用于串联。
You can even use String.plus()
method.
你甚至可以使用String.plus()
方法。
回答by Rhusfer
I agree with the accepted answer above but it is only good for known string values. For dynamic string values here is my suggestion.
我同意上面接受的答案,但它只适用于已知的字符串值。对于动态字符串值,这是我的建议。
// A list may come from an API JSON like
{
"persons": {
"Person 1",
"Person 2",
"Person 3",
...
"Person N"
}
}
var listOfNames = mutableListOf<String>()
val stringOfNames = listOfNames.joinToString(", ")
// ", " <- a separator for the strings, could be any string that you want
// Posible result
// Person 1, Person 2, Person 3, ..., Person N
This is useful for concatenating list of strings with separator.
这对于用分隔符连接字符串列表很有用。
回答by CoolMind
Similar to @Rhusfer answerI wrote this. In case you have a group of EditText
s and want to concatenate their values, you can write:
类似于@Rhusfer 的回答我写了这个。如果您有一组EditText
s 并且想要连接它们的值,您可以编写:
listOf(edit_1, edit_2, edit_3, edit_4).joinToString(separator = "") { it.text.toString() }
If you want to concatenate HashMap
, use this:
如果要连接HashMap
,请使用以下命令:
map.entries.joinToString(separator = ", ")
// Result:
// id=123, name=John, surname=Smith
回答by Licat Julius
Try this, I think this is a natively way to concatenate strings in Kotlin:
试试这个,我认为这是在 Kotlin 中连接字符串的本机方式:
val result = buildString{
append("a")
append("b")
}
println(result)
// you will see "ab" in console.
回答by Shradha Sangtani
There are various way to concatenate strings in kotlin Example -
在 kotlin 示例中有多种连接字符串的方法 -
a = "Hello" , b= "World"
Using + operator
a+b
Using
plus()
operatora.plus(b)
使用 + 运算符
a+b
使用
plus()
运算符a.plus(b)
Note - + is internally converted to .plus() method only
注意 - + 仅在内部转换为 .plus() 方法
In above 2 methods, a new string object is created as strings are immutable. if we want to modify the existing string, we can use StringBuilder
在上述 2 种方法中,由于字符串是不可变的,因此创建了一个新的字符串对象。如果我们想修改现有的字符串,我们可以使用 StringBuilder
StringBuilder str = StringBuilder("Hello").append("World")
回答by Kanagalingam
yourString += "newString"
yourString += "newString"
This way you can concatenate a string
这样你就可以连接一个字符串