1

I need to create a template in Kotlin which adds parameters to it at different locations.

Suppose I have a template like this

val template = """Hi $user, 
Following channels are Private, $commaSeparatedChannelNames
Please consider adding channel manually using \invite $botName inside channel """ 

Then it only adds to the template all the params if they are ready and not at a later stage , whenever I need to add it.

For example in python, we can use

template = """Hi {user}, 
Following channels are Private, {channel_names}
Please consider adding channel manually using \invite {bot_name} inside channel """

template.format(user=user, channel_names = comma_separated_channel_names, bot_name = bot_name)

Is there something analogous to above in Kotlin which can be used.

Any help is appreciated

1 Answer 1

4

You could use the String.format method:

val template = """
    Hi %s, 
    Following channels are Private, %s
    Please consider adding channel manually using \invite %s inside channel 
"""

print(String.format(template, "Example", listOf("Channel 1", "Channel 2", "Channel 3").joinToString(", "), "Test"))

// Hi Example, 
// Following channels are Private, Channel 1, Channel 2, Channel 3
// Please consider adding channel manually using \invite Test inside channel 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.