String Interpolation with format variable

后端 未结 6 1994
心在旅途
心在旅途 2020-11-27 22:08

I can do this:

var log = string.Format(\"URL: {0}\", url);

or even like this

var format = \"URL: {0}\";
...
var log = strin         


        
6条回答
  •  余生分开走
    2020-11-27 22:39

    More of an idea as opposed to an answer.

    For the example shown in the question, you can do the following.

    var format = "URL: ";
    ...
    var url = "http://google.com";
    ...
    var result= $"{format} {url}";
    

    I have an actual project where I have to do something like this a lot:

    var label = "Some Label";
    var value = "SomeValue";
    
    //both label & value are results of some logic
    
    var result = $"{label}: {value}";
    

提交回复
热议问题