Hey I am working in list of object. I want to iterate whole object one by one and print every index value in each line. I tried some code but I am not getting the proper output.
StringPrint.kt
fun main() {
val newString = createData().joinToString("\n") { "${it.title?: ""} ${it.status?: ""}".trim() }
println(newString)
}
data class Event(val title: String? = null, val status: String? = null)
fun createData() = listOf(
Event("text 1", "abc"),
Event("text 2", "abc"),
Event("text 3", "abc"),
Event("text 4", "abc"),
Event("", null),
Event(null, ""),
Event(null, "3"),
Event("null", ""),
Event("456", "89")
)
output
text 1 abc
text 2 abc
text 3 abc
text 4 abc
Space Space
Space Space
3
null
456 89
As you can see in output I added space keyword which means there is whiteSpace character is present and null. I don't want all these.
Expected Output
text 1 abc
text 2 abc
text 3 abc
text 4 abc
3
456 89
After @lukas.j suggestion I tried this
val newString = createData()
.filterNot { it.title.isNullOrBlank() || it.status.isNullOrBlank() }
.joinToString("\n") { "${it.title?: ""} ${it.status?: ""}".trim() }
println(newString)
Output.kt
text 1 abc
text 2 abc
text 3 abc
text 4 abc
456 89
it's not printing value 3 in console.
CodePudding user response:
val newString = createData()
.filterNot { it.title.isNullOrBlank() && it.status.isNullOrBlank() }
.joinToString("\n") { "${it.title?: ""} ${it.status?: ""}".trim() }