ways to escape { in string.format function in vb

后端 未结 3 1505
隐瞒了意图╮
隐瞒了意图╮ 2021-01-13 07:06

I am facing an issue with my code .

the thing is i want to escape { in the string.format function ...

Of course one way to escape it is to use {{ (2 curly br

相关标签:
3条回答
  • 2021-01-13 07:40

    Why would there be another solution? {{ is specified as the way of escaping braces in a format string... I can't see why the API designers would include another way.

    Of course, you could also provide a format parameter and then populate it with a brace:

    Dim text as String = string.Format("{0}{1}", "{", "}")
    

    will give "{}" as a string, for example... but I can't see that this is a better solution.

    Perhaps if you could say why you dislike the normal solution (double curly braces) we could advise you on an alternative - but there isn't one within the format specification "language" itself, as far as I'm aware.

    0 讨论(0)
  • 2021-01-13 07:53

    No, unfortunately it is not possible.

    0 讨论(0)
  • 2021-01-13 08:05

    Well, you could do it this way, but not a good idea in my view.

        Dim str1 As String = "Print this " & Chr(123) & "0" & Chr(125) & " string"
        Dim str2 As String = "silly"
        Console.WriteLine(String.Format(str1, str2))
        Console.ReadLine()
    
    0 讨论(0)
提交回复
热议问题