Skip to content

Instantly share code, notes, and snippets.

@nadar71
Last active June 14, 2020 05:52
Show Gist options
  • Save nadar71/15ccfcd9286db7fca1733b7a9d7c5149 to your computer and use it in GitHub Desktop.
Save nadar71/15ccfcd9286db7fca1733b7a9d7c5149 to your computer and use it in GitHub Desktop.
Kotlin, ArrayList<String> to String comma delimited and viceversa
fun ArrayListStringToStringComma(arrayList: ArrayList<String>):String{
var result = ""
for (i in arrayList.indices) {
if (i >= 1 && i <= arrayList.size-1) result += ","
result += arrayList[i]
}
return result
}
// The opposite of previous
fun StringCommaToArrayListString(listString: String ):ArrayList<String>{
val result = ArrayList<String>()
for (w in listString.trim(' ').split(","))
if (w.isNotEmpty()) result.add(w)
return result
}
fun main(args: Array<String>) {
val arr = ArrayList<String>()
arr.add("cane")
arr.add("gatto")
arr.add("topo")
arr.add("leone")
val res: String = ArrayListStringToStringComma(arr)
println("from arr to string : $res")
val ARR = StringCommaToArrayListString(res)
for(i in ARR){
println("from string to arr : ${i}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment