Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda