gpt4 book ai didi

c# - 添加了来自变量的文本的多行字符串

转载 作者:IT王子 更新时间:2023-10-29 04:22:36 24 4
gpt4 key购买 nike

我知道这会起作用:

string multiline_text = @"this is a multiline text
this is line 1
this is line 2
this is line 3";

我怎样才能完成以下工作:

string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";

string multiline_text = @"this is a multiline text
this is " + a1 + "
this is " + a2 + "
this is " + a3 + ";

是否可以不将字符串拆分为多个子字符串,每行一个?

最佳答案

一种选择是改用字符串格式。在 C# 6 之前:

string pattern = @"this is a multiline text
this is {0}
this is {1}
this is {2}";

string result = string.Format(pattern, a1, a2, a3);

在 C# 6 中,您可以使用内插的逐字字符串文字:

string pattern = $@"this is a multiline text
this is {a1}
this is {a2}
this is {a3}";

请注意 $@ 必须完全一样 - 如果您尝试使用 @$,它将无法编译。

关于c# - 添加了来自变量的文本的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com