c# string.format 中使用$的坑

            string a = "hello}";
            string s = string.Format($"{a}");
上面这种情况,会报格式错误,会把hello}中的}当做一个字符串结束;

 结果: 

 

修改:
            string a = "hello}";
            string s = string.Format("{0}",a);

  结果:

 

使用了$ 实际上是字符串直接写入 如:$"{a}" 相当于$"{hello}}"从而导致格式错误

 

posted @ 2021-04-21 20:52  darkif  阅读(403)  评论(0编辑  收藏  举报