Home > Blockchain >  Creating a string template in kotlin to add parameters later
Creating a string template in kotlin to add parameters later

Time:04-02

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

CodePudding user response:

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 
  • Related