joinToString

suspend fun <T> Flow<T>.joinToString(f: suspend (T) -> String = { it.toString() }): String

Joins the elements of the Flow into a single string using the provided f function to transform each element to a string.

Return

A String containing the concatenated strings of the flow elements.

Example usage:

flowOf(1, 2, 3, 6, 5, 4)
.joinToString { "$it" }
.also(::println) //123654

Parameters

f

A suspend function that transforms an element of type T to a String. Defaults to calling toString() on the element.


suspend fun <T> Flow<T>.joinToString(between: String, f: suspend (T) -> String): String

Joins the elements of the Flow into a single string, separated by the specified between string, using the provided f function to transform each element to a string.

Return

A String containing the concatenated strings of the flow elements, separated by the between string.

Example usage:

flowOf(1, 2, 3, 6, 5, 4)
.joinToString(", ") { "$it" }
.also(::println) //1, 2, 3, 6, 5, 4

Parameters

between

A String used as a separator between the flow elements.

f

A suspend function that transforms an element of type T to a String.


suspend fun <T> Flow<T>.joinToString(between: String, start: String, end: String, f: suspend (T) -> String): String

Joins the elements of the Flow into a single string, enclosed by the specified start and end strings, and separated by the specified between string. Uses the provided f function to transform each element to a string.

Return

A String containing the concatenated strings of the flow elements, enclosed by start and end, and separated by the between string.

Example usage:

flowOf(1, 2, 3, 6, 5, 4)
.joinToString("[", ", ", "]") { "$it" }
.also(::println) //[1, 2, 3, 6, 5, 4]

Parameters

start

A String used at the beginning of the resulting string.

between

A String used as a separator between the flow elements.

end

A String used at the end of the resulting string.

f

A suspend function that transforms an element of type T to a String.