I rather have this ugly way of building a string from a list as:
val input = listOf("[A,B]", "[C,D]")
val builder = StringBuilder()
builder.append("Serialized('IDs((")
for (pt in input) {
builder.append(pt[0] " " pt[1])
builder.append(", ")
}
builder.append("))')")
The problem is that it adds a comma after the last element and if I want to avoid that I need to add another if check in the loop for the last element.
I wonder if there is a more concise way of doing this in kotlin?
EDIT
End result should be something like:
Serialized('IDs((A B,C D))')
CodePudding user response:
In Kotlin you can use joinToString for this kind of use case (it deals with inserting the separator only between elements).
It is very versatile because it allows to specify a transform function for each element (in addition to the more classic separator
, prefix
, postfix
). This makes it equivalent to mapping all elements to strings and then joining them together, but in one single call.
If input
really is a List<List<String>>
like you mention in the title and you assume in your loop, you can use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { (x, y) -> "$x $y" }
Note that the syntax with (x, y)
is a destructuring syntax that automatically gets the first and second element of the lists inside your list (parentheses are important).
If your input is in fact a List<String>
as in listOf("[A,B]", "[C,D]")
that you wrote at the top of your code, you can instead use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { it.removeSurrounding("[", "]").replace(",", " ") }
CodePudding user response:
val input = listOf("[A,B]", "[C,D]")
val result =
"Serialized('IDs(("
input.joinToString(",") { it.removeSurrounding("[", "]").replace(",", " ") }
"))')"
println(result) // Output: Serialized('IDs((A B,C D))')
CodePudding user response:
Kotlin provides an extension function [joinToString][1]
(in Iterable
) for this type of purpose.
input.joinToString(",", "Serialized('IDs((", "))')")
This will correctly add the separator.