最新消息:20210917 已从crifan.com换到crifan.org

【已解决】C#中字符串格式化中包含花括号

字符串 crifan 697浏览 0评论
C#中,想要去打印字符串,其中包括花括号,去试了试:
           foreach (double curItem1Value in outputList1)
           {
               Console.WriteLine("[{0}]=\{1}, {2}\}", curIdx, curItem1Value,  outputList2[curIdx]);
               curIdx++;
           }
结果直接语法报错,没法通过编译
无法识别的转义序列
然后换用
               Console.WriteLine("[{0}]={{1}, {2}}", curIdx, curItem1Value,  outputList2[curIdx]);
结果运行时报错
引发的异常:“System.FormatException”(位于 mscorlib.dll 中)
“System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生
输入字符串的格式不正确。
c# string format {
C# String.Format格式说明 – zbwmx的专栏 – CSDN博客
C#中string.Format输出内容中含有花括号的解决方法 – lzdidiv的专栏 – CSDN博客
“用两个连在一起的“{{”表示“{”,右花括号也同理”
【总结】
C#中字符串格式化中带花括号时,用两个连续的左花括号或右花括号,即可表示 左花括号和右花括号
Console.WriteLine("[{0}]={{{1}, {2}}}", curIdx, curItem1Value,  outputList2[curIdx]);
即可正常输出

转载请注明:在路上 » 【已解决】C#中字符串格式化中包含花括号

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.209 seconds, using 23.33MB memory