You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#格式化字符串的消息模板中为文本设置加粗格式

在C#中格式化带加粗效果的字符串模板

要实现将模板里的“Code”设置为HTML加粗格式,直接修改模板字符串即可,把“Code”用<strong>标签包裹,再通过string.Format填充参数。

示例代码:

// 修改后的模板
string template = "<strong>Code</strong>: {0}\nCreation Date: {1}";
// 填充参数
string result = string.Format(template, "123", "2024-05-20");
// 输出结果为:<strong>Code</strong>:123
// Creation Date: 2024-05-20

如果是在非HTML环境(比如控制台)实现类似加粗的视觉效果,可借助输出环境的特性,比如控制台通过修改文字颜色模拟:

Console.Write("<strong>Code</strong>:");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("123");
Console.ResetColor();
Console.WriteLine("\nCreation Date: 2024-05-20");

内容的提问来源于stack exchange,提问作者kimiahdri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 02:32:02