Last active
June 14, 2020 05:52
-
-
Save nadar71/15ccfcd9286db7fca1733b7a9d7c5149 to your computer and use it in GitHub Desktop.
Kotlin, ArrayList<String> to String comma delimited and viceversa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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