string.format format string containing {

后端 未结 5 754
半阙折子戏
半阙折子戏 2020-12-07 04:31

Is there a way for the format string used in a call to string.format to contain a { character that is not used for substring insertion?

For example how could I do th

相关标签:
5条回答
  • 2020-12-07 05:15

    Yup, you just need to double it:

    string.Format("my format has this {{ in it {0}", abc);
    

    See the "escaping braces" section on the MSDN page for Composite Formatting for more details.

    0 讨论(0)
  • 2020-12-07 05:16

    Here's an article that explains how to escape the curly brace in string.format with a few examples and some good reading.

    http://msdn.microsoft.com/en-us/netframework/aa569608#Question2

    EDIT: Adding useful text to accompany link :)

    0 讨论(0)
  • 2020-12-07 05:19

    Double it up to escape it. So "{{" will work properly and will output from the string.format as a single "{"

    0 讨论(0)
  • 2020-12-07 05:21

    it is called escaping. mostly you do a \ in front of the odd char

    0 讨论(0)
  • 2020-12-07 05:29

    Use double {{ brace in your format string.

    0 讨论(0)
提交回复
热议问题