Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Last active February 7, 2019 07:30
Show Gist options
  • Save renaudcerrato/0c10ee7e8d9f125ab30e1b1aadd7235e to your computer and use it in GitHub Desktop.
Save renaudcerrato/0c10ee7e8d9f125ab30e1b1aadd7235e to your computer and use it in GitHub Desktop.
Kotlin Functions, advanced
fun join(strings: Collection<String>, separator: String = " ",
prefix: String = "", postfix: String = "") : String
{
val result = StringBuilder(prefix)
for((index, element) in strings.withIndex()) {
if(index > 0) result.append(separator)
result.append(element)
}
result.append(postfix)
return result.toString()
}
join(list)
join(list, separator = ",")
join(list, prefix = "(", postfix = ")")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment