Add an optional parameter to joinToString() and joinTo() functions: lambda function which maps the element to string.
#KT-5468 Fixed
This commit is contained in:
@@ -267,4 +267,10 @@ abstract class IterableTests<T : Iterable<String>>(val data: T, val empty: T) {
|
||||
assertEquals(6, reduced.length())
|
||||
assertTrue(reduced == "foobar" || reduced == "barfoo")
|
||||
}
|
||||
|
||||
Test
|
||||
fun mapAndJoinToString() {
|
||||
val result = data.joinToString(separator = "-") { it.toUpperCase() }
|
||||
assertEquals("FOO-BAR", result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,10 @@ public class SequenceTest {
|
||||
}
|
||||
}
|
||||
|
||||
test fun mapAndJoinToString() {
|
||||
assertEquals("3, 5, 8", fibonacci().withIndex().filter { it.index > 3 }.take(3).joinToString { it.value.toString() })
|
||||
}
|
||||
|
||||
test fun withIndex() {
|
||||
val data = sequenceOf("foo", "bar")
|
||||
val indexed = data.withIndex().map { it.value.substring(0..it.index) }.toList()
|
||||
|
||||
@@ -240,6 +240,10 @@ class StringJVMTest {
|
||||
val data2 = "verylongstring".toList()
|
||||
val result2 = data2.joinToString("-", "[", "]", 11, "oops")
|
||||
assertEquals("[v-e-r-y-l-o-n-g-s-t-r-oops]", result2)
|
||||
|
||||
val data3 = "a1/b".toList()
|
||||
val result3 = data3.joinToString() { it.toUpperCase().toString() }
|
||||
assertEquals("A, 1, /, B", result3)
|
||||
}
|
||||
|
||||
test fun join() {
|
||||
|
||||
Reference in New Issue
Block a user