How do I interpolate strings?

前端 未结 15 774
感情败类
感情败类 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 05:52

    You can use the following way

    String interpolation

    The $ special character identifies a string literal as an interpolated string. e.g.

    string name = "Mark";
    string surname = "D'souza";
    WriteLine($"Name :{name} Surname :{surname}" );//Name :Mark Surname :D'souza  
    

    An interpolated string is a string literal that might contain interpolated expressions. When an interpolated string is resolved to a result string, items with interpolated expressions are replaced by the string representations of the expression results.

    String.Format

    Use String.Format if you need to insert the value of an object, variable, or expression into another string.E.g.

    WriteLine(String.Format("Name: {0}, Surname : {1}", name, surname));
    

提交回复
热议问题