Skip to content

Instantly share code, notes, and snippets.

@socar-doga
Created January 15, 2018 02:28
Show Gist options
  • Save socar-doga/5465476c62a6df992924abe50a2019a9 to your computer and use it in GitHub Desktop.
Save socar-doga/5465476c62a6df992924abe50a2019a9 to your computer and use it in GitHub Desktop.
Using joinToString, removePrefix, removeSuffix and split with Kotlin
class StringTest {
private fun stringList(){
val ids = "[57670,57669,57668,57667,57666,57665,57664,57663,57661,57662,57659]"
val personList = mutableListOf<Person>(
Person("Dave", "Kingdom", 23),
Person("Joshi", "Delete", 30),
Person("Kora", "Rabbit", 27),
Person("Kurt", "Banana", 30),
Person("Jack", "Reverse", 17)
)
val text = "[String Test]\n" +
"|- Date : 2017-11-01 16:30\n" +
"|- Place : Seoul \n"
val convertTwo = personList.joinToString(separator = "\n", prefix = text){ item->
"name : ${item.name}, nickName : ${item.nick}"
}
println(getDevidedArray(ids))
println(convertTwo)
}
private fun getDevidedArray(data : String):List<String>{
return data.removePrefix("[")
.removeSuffix("]")
.split(",")
}
data class Person(
val name : String,
val nick : String,
val age : Int
)
companion object{
@JvmStatic fun main(args : Array<String>) {
KotlinTest().stringList()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment