How to provide custom string placeholder for string format

后端 未结 8 2128
借酒劲吻你
借酒劲吻你 2020-12-24 07:15

I have a string

string str =\"Enter {0} patient name\";

I am using string.format to format it.

String.Format(str, \"Hello\         


        
相关标签:
8条回答
  • 2020-12-24 07:47

    I wanted something that worked more like Python's string formatting, so I wrote this: https://gist.github.com/samwyse/b225b32ae1aea6fb27ad9c966b9ca90b

    Use it like this:

    Dim template = New FormatFromDictionary("{cat} vs {dog}")
    Dim d = New Dictionary(Of String, Object) From {
        {"cat", "Felix"}, {"dog", "Rex"}}
    Console.WriteLine(template.Replace(d)) ' Felix vs Rex
    
    0 讨论(0)
  • 2020-12-24 07:48

    I saw all the answers above, yet, couldn't get the question right :)

    Is there any particular reason why the following code does not meet your requirement?

    string myFirstStr = GetMyFirstStrFromSomewhere();
    string mySecondStr = GetMySecondStrFromSomewhere();
    
    string result = "Enter " + myFirstStr + " " + mySecondStr + " name";
    
    0 讨论(0)
提交回复
热议问题