Building strings based on dataset elements
Sometimes, we face the problem of generating text based on collections' elements. This is where the Iterable.joinToString()
extension function can help. For example, we can consider working on an email-message-forwarding feature. When a user clicks the forward button, the original message's body text is concatenated, with the prefix looking something like this:
<br/> <p>---------- Forwarded message ----------</p> <p> From: [email protected] <br/> Date: 14/04/2000 <br/> Subject: Any plans for the evening?<br/> To: [email protected], [email protected]<br/> </p>
In this recipe, we are going to implement a function that is going to generate the recipients' string, for example:
To: [email protected], [email protected]</br>
For a given list of Address
type objects, it is defined as follows:
data class Address(val emailAddress: String, val displayName: String)
How to do it...
- Declare the
generateRecipientsString...