我需要在 Kotlin 中創建一個模板,在不同的位置向它添加引數。
假設我有一個這樣的模板
val template = """Hi $user,
Following channels are Private, $commaSeparatedChannelNames
Please consider adding channel manually using \invite $botName inside channel """
然后它只會將所有引數添加到模板中,如果它們已準備好而不是在稍后階段,只要我需要添加它。
例如在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)
在 Kotlin 中是否有類似的東西可以使用。
任何幫助表示贊賞
uj5u.com熱心網友回復:
您可以使用以下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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454179.html
標籤:科特林
