Eliuber
Eliuber

Reputation: 88

Interpolate variables in a string

How can i interpolate the variable userName in the string.Im using C#.I added \ but it does not found

 String orderStr = String.Format(@"{
                  ""currency"":""MXN"",
                  ""customer_info"": {
                    ""name"": ""julio"",
                    ""phone"": ""Cabalos"",
                    ""email"": ""[email protected]""
                  },
                  ""line_items"": [{
                    ""name"": ""\'{0}\'"",
                    ""description"": ""descripc"",
                    ""unit_price"": 233,
                    ""quantity"": '1',
                    ""tags"": [""Transporte"", ""Logistic Cloud""],
                    ""type"": ""physical""
                  }],
                 ""charges"":[{
                    ""payment_method"": {
                      ""type"": ""oxxo_cash""
                    }
                  }]
               }", userName);

Upvotes: 0

Views: 62

Answers (1)

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

You have to escape the { and } with {{ and }}. Also you can use interoplated string ($)

string userName = "userName";
String orderStr = $@"{{
      ""currency"":""MXN"",
      ""customer_info"": {{
        ""name"": ""julio"",
        ""phone"": ""Cabalos"",
        ""email"": ""[email protected]""
      }},
      ""line_items"": [{{
        ""name"": ""\'{userName}\'"",
        ""description"": ""descripc"",
        ""unit_price"": 233,
        ""quantity"": '1',
        ""tags"": [""Transporte"", ""Logistic Cloud""],
        ""type"": ""physical""
      }}],
     ""charges"":[{{
        ""payment_method"": {{
          ""type"": ""oxxo_cash""
        }}
      }}]
   }}";

Upvotes: 1

Related Questions