Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Building string from list of list of strings

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))')
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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(",", " ") }
over 4 years ago · Santiago Trujillo Report

0

Kotlin provides an extension function [joinToString][1] (in Iterable) for this type of purpose.

input.joinToString(",", "Serialized('IDs((", "))')")

This will correctly add the separator.

over 4 years ago · Santiago Trujillo Report

0

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))')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!