String Interpolation with format variable

后端 未结 6 1976
心在旅途
心在旅途 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:34

    It seems that you can do it like this:

    var googleUrl = "http://google.com";
    var url = $"URL: {googleUrl}";
    
    System.Console.WriteLine(url);
    

    You can check for more details in https://msdn.microsoft.com/en-us/library/dn961160.aspx

    0 讨论(0)
  • 2020-11-27 22:36

    One approach to work around that would be to use a lambda containing the interpolated string. Something like:

    Func<string, string> formatter = url => $"URL: {url}";
    ...
    var googleUrl = "http://google.com";
    ...
    var log = formatter(googleUrl);
    

    In C# 7.0, you could use a local function instead of a lambda, to make the code slightly simpler and more efficient:

    string formatter(string url) => $"URL: {url}";
    ...
    var googleUrl = "http://google.com";
    ...
    var log = formatter(googleUrl);
    
    0 讨论(0)
  • 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}";
    
    0 讨论(0)
  • 2020-11-27 22:50

    No, you can't use string interpolation with something other than a string literal as the compiler creates a "regular" format string even when you use string interpolation.

    Because this:

    string name = "bar";
    string result = $"{name}";
    

    is compiled into this:

    string name = "bar";
    string result = string.Format("{0}", name);
    

    the string in runtime must be a "regular" format string and not the string interpolation equivalent.

    You can use the plain old String.Format instead.

    0 讨论(0)
  • 2020-11-27 22:53

    String interpolation is a compiler, not library, feature.

    The holes are not names, but expressions:

    var r = new Rectangle(5, 4);
    var s = $"area: {r.Width + r.Heigh}":
    

    How would you do that for localization, as you intend to?

    Even r only exists at compile time. In IL it's just a position on the method's variable stack.

    I've done what you intend to do for resources and configuration files.

    Since you can only have a finite set of "variables" to substitute, what I did was have an array (or dictionary, if you prefer) and use a regular expression to replace the names in the holes with its index. What I did even allowed for format specifiers.

    0 讨论(0)
  • 2020-11-27 22:58

    This is supposed to be a comment to the answer from i3arnon but I do not have the reputation :-( : But for those who come to this old thread, in string.Format the format can be a variable:

    string name = "bar";
    string format = "{0}";
    string result = string.Format(format, name);
    

    works.

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