Kotlin Sequence joinToString Function

The joinToString function in Kotlin is used to create a single string from the elements of a sequence by concatenating them, using a specified separator and optional prefix, postfix, limit, truncated, and transform function. It is part of the Kotlin standard library and provides a flexible way to convert sequences into formatted strings.

Table of Contents

  1. Introduction
  2. joinToString Function Syntax
  3. Understanding joinToString
  4. Examples
    • Basic Usage
    • Custom Separator, Prefix, and Postfix
    • Limiting the Number of Elements
    • Custom Transform Function
  5. Real-World Use Case
  6. Conclusion

Introduction

The joinToString function allows you to create a single string from the elements of a sequence by concatenating them with a specified separator. This is useful for scenarios where you need to display sequence elements as a formatted string.

joinToString Function Syntax

The syntax for the joinToString function is as follows:

fun <T> Sequence<T>.joinToString( separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null ): String 

Parameters:

  • separator: A string to separate each element (default is ", ").
  • prefix: A string to prepend to the result (default is "").
  • postfix: A string to append to the result (default is "").
  • limit: The maximum number of elements to include in the result (default is -1, meaning no limit).
  • truncated: A string to represent omitted elements when limit is reached (default is "...").
  • transform: A function to transform each element into a string before joining (default is null).

Returns:

  • A string representing the sequence elements joined with the specified parameters.

Understanding joinToString

The joinToString function works by iterating through the sequence and applying the specified parameters to concatenate the elements into a single string. It uses the separator between elements, adds the prefix at the beginning and the postfix at the end, limits the number of elements if specified, and applies the transform function to each element if provided.

Examples

Basic Usage

To demonstrate the basic usage of joinToString, we will create a sequence of integers and join them into a single string.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5) val result = numbers.joinToString() println(result) // Output: 1, 2, 3, 4, 5 } 

Output:

1, 2, 3, 4, 5 

Custom Separator, Prefix, and Postfix

This example shows how to customize the separator, prefix, and postfix.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val result = names.joinToString(separator = " | ", prefix = "[", postfix = "]") println(result) // Output: [Arjun | Bhaskar | Chitra | Deepak | Esha] } 

Output:

[Arjun | Bhaskar | Chitra | Deepak | Esha] 

Limiting the Number of Elements

This example shows how to limit the number of elements included in the result.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = numbers.joinToString(limit = 5) println(result) // Output: 1, 2, 3, 4, 5, ... } 

Output:

1, 2, 3, 4, 5, ... 

Custom Transform Function

This example shows how to use a custom transform function to format each element before joining.

Example

data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22) ) val result = people.joinToString(transform = { "${it.name} (${it.age})" }) println(result) // Output: Arjun (25), Bhaskar (30), Chitra (22) } 

Output:

Arjun (25), Bhaskar (30), Chitra (22) 

Real-World Use Case

Generating CSV Output

In real-world applications, the joinToString function can be used to generate CSV output from a sequence of data.

Example

data class Product(val name: String, val price: Double) fun main() { val products = sequenceOf( Product("Laptop", 1000.0), Product("Smartphone", 600.0), Product("Tablet", 300.0) ) val header = "Product Name,Price" val csv = products.joinToString(separator = "\n", prefix = "$header\n") { "${it.name},${it.price}" } println(csv) // Output: // Product Name,Price // Laptop,1000.0 // Smartphone,600.0 // Tablet,300.0 } 

Output:

Product Name,Price Laptop,1000.0 Smartphone,600.0 Tablet,300.0 

Conclusion

The joinToString function in Kotlin provides used for creating formatted strings from sequences. By understanding and using the joinToString function, you can efficiently generate string representations of sequence elements in your Kotlin applications, ensuring that your data is displayed in a readable and customizable format.

Leave a Comment

Scroll to Top