如何将普通字符串转换为多行字符串

时间:2016-07-26 09:04:22

标签: arrays string formatting multilinestring

如何将字符串转换为格式化字符串

关注字符串

  

"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us"

我必须像这样转换上面的字符串

"It was the best of times, it was the worst of times,\\n" +
" it was the age of wisdom, it was the age of foolishness,\\n" +
" it was the epoch of belief, it was the epoch of incredulity,\\n" +
" it was the season of Light, it was the season of Darkness,\\n" +
" it was the spring of hope, it was the winter of despair,\\n" +
" we had everything before us, we had nothing before us";

1 个答案:

答案 0 :(得分:0)

只需在字符串中添加一个\r\n而不是+的转义序列。另见:https://msdn.microsoft.com/en-us/library/h21280bw.aspx

"It was the best of times, it was the worst of times,\r\n
  it was the age of wisdom, it was the age of foolishness,\r\n
  it was the epoch of belief, it was the epoch of incredulity,\r\n
  it was the season of Light, it was the season of Darkness,\r\n
  it was the spring of hope, it was the winter of despair,\r\n
  we had everything before us, we had nothing before us"

或使用Environment.NewLine属性,如

"It was the best of times, it was the worst of times," + Environment.NewLine
+ " it was the age of wisdom, it was the age of foolishness," + Environment.NewLine 
+ " it was the epoch of belief, it was the epoch of incredulity," + Environment.NewLine 
+ " it was the season of Light, it was the season of Darkness," + Environment.NewLine 
+ " it was the spring of hope, it was the winter of despair," + Environment.NewLine  
+ " we had everything before us, we had nothing before us"