Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda