How do I interpolate strings?

前端 未结 15 704
感情败类
感情败类 2020-11-27 05:38

I want to do the following in C# (coming from a Python background):

strVar = \"stack\"
mystr  = \"This is %soverflow\" % (strVar)

How do I

相关标签:
15条回答
  • 2020-11-27 06:12
    string mystr = string.Format("This is {0}overflow", strVar);
    

    And you could also use named parameters instead of indexes.

    0 讨论(0)
  • 2020-11-27 06:14

    You should be using String.Format(). The syntax is a bit different, numerical placeholders are used instead.

    Example:

    String.Format("item {0}, item {1}", "one", "two")
    

    Have a look at http://msdn.microsoft.com/en-us/library/system.string.format.aspx for more details.

    0 讨论(0)
  • 2020-11-27 06:16

    Use:

    strVar = "stack"
    mystr  = String.Format("This is {0}", strVar);
    
    0 讨论(0)
提交回复
热议问题