【发布时间】:2019-03-20 06:03:04
【问题描述】:
我正在尝试从字符串数组创建多行字符串 喜欢
@"A minErr message has two parts:
the message itself and the url that contains the encoded message.
The message's parameters can contain other error messages which also include error urls.";
我有这些行的字符串数组
string [] miltilines={"A minErr message has two parts:","the message itself and the url that contains the encoded message.","The message's parameters can contain other error messages which also include error urls."}
我尝试了多种方法来获取多行字符串对象,但在字符串对象中以 \r\n 结束
1:String.Join(Environment.NewLine, miltilines)
2:
string multiline = @" ";
foreach (var item in miltilines)
{
multiline += @"" + item.Trim() + " ";
}
3:
StringBuilder stringBuilder = new StringBuilder();
foreach (var item in miltilines)
{
stringBuilder.AppendLine(item );
}
有没有办法从字符串数组中获取多行字符串对象
【问题讨论】:
-
#1 应该可以正常工作。是什么让你认为它没有
-
我已经尝试过了,在对象中我得到“一条 minErr 消息有两部分:/r/n 消息本身和包含编码消息的 url。/r/n 消息的参数可以包含其他错误消息,其中也包括错误 url。"
-
but ended with \r\n in string object这就是在 Visual Studio 调试窗口中表示换行符的方式。如果您将该字符串写入文件并在文本编辑器中打开,您将看到换行符。 -
如果第三方工具在字符串包含换行符时给您异常,那么您不能传入多行字符串。多行字符串按定义包含
\r\n。 -
从您的样本中形成的字符串也包含
\r\n。您刚刚使用@隐藏字符并在编辑器中显示格式化字符串。在该行之后设置一个断点并在调试器中查看该字符串。
标签: c# multilinestring