简体   繁体   English

在kotlin中创建字符串模板,用于后面添加参数

[英]Creating a string template in kotlin to add parameters later

I need to create a template in Kotlin which adds parameters to it at different locations.我需要在 Kotlin 中创建一个模板,它在不同的位置向它添加参数。

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例如python,我们可以使用

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. Kotlin中是否有类似于上面的东西可以使用。

Any help is appreciated任何帮助表示赞赏

You could use the String.format method:您可以使用String.format方法:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM