【问题标题】:Type converting a string to a multiline version键入将字符串转换为多行版本
【发布时间】:2026-01-04 16:20:04
【问题描述】:

我有

 string subquery="cmd cmd";

我需要

string query = @"asd 
asd  asd

asd asd
"+ MakeMultiline(subquery) + @"asd asd asd
asd asd where id=@id";

可以不改变子查询字符串的声明吗?

我需要这个,因为当我尝试时它会给出未完成的字符串文字错误。

【问题讨论】:

  • 您的意思完全不清楚 - 我们不知道 MakeMultiline 做了什么,或者您在哪里得到错误。请展示一个简短但完整的程序来说明问题。
  • MakeMultiline方法的定义是什么?
  • 不是真的,我放的只是例子。仅使用字符串文字进行了尝试。真正的字符串查询是页长的。

标签: c# string type-conversion multilinestring


【解决方案1】:

使用 Environment.NewLine 或使用 \n \r\n。

例子:

string query = "asd \n asd  asd \n asd asd"+ MakeMultiline(subquery) +
 "asd asd asd \n asd asd where id=@id";

不要使用@来使用逐字字符串,除非你想定义两次转义序列。

【讨论】: