Resolved How to concat @ string

rashidbilgrami

New member
Joined
Feb 20, 2023
Messages
3
Programming Experience
10+
Can any one help me to integrate the variable in the blow code
using (var content = new StringContent(@"{
""userName"": ""userValue_Valiable"",
""numbers"": ""NumberValue"",
""userSender"": ""SnameValkue"",
""apiKey"": ""apiValue"",
""msg"": ""messageVarukavke""
}", System.Text.Encoding.Default, "application/json"))
 
Solution
Thanks for a reply but i want to add the "" also with the string like this
""userName"": ""userValue_Valiable"",

You're already doing that so what's the problem? All you need to do is:
  1. Double up the braces in the string you have.
  2. Add a $ symbol at the beginning.
  3. Replace any existing substring with a variable wrapped in braces.
Everything else will just work as it is. That said, if you're writing JSON, you really ought to use the tools intended for the job, as recommended by cjard.
You can use string interpolation with a verbatim string literal in exactly the same way as you do any other string literal. The one point to note is that literal braces in an interpolated string must be escaped with another brace.
C#:
var name = "John Smith";
var str = $@"{{Hello.
Pleased to meet you {name}.}}";

Console.WriteLine(str);
That will display the following:
{Hello.
Pleased to meet you John Smith.}
 
Thanks for a reply but i want to add the "" also with the string like this
""userName"": ""userValue_Valiable"",

You're already doing that so what's the problem? All you need to do is:
  1. Double up the braces in the string you have.
  2. Add a $ symbol at the beginning.
  3. Replace any existing substring with a variable wrapped in braces.
Everything else will just work as it is. That said, if you're writing JSON, you really ought to use the tools intended for the job, as recommended by cjard.
 
Solution
You're already doing that so what's the problem? All you need to do is:
  1. Double up the braces in the string you have.
  2. Add a $ symbol at the beginning.
  3. Replace any existing substring with a variable wrapped in braces.
Everything else will just work as it is. That said, if you're writing JSON, you really ought to use the tools intended for the job, as recommended by cjard.

Thanks it resolved my problem (y)
 
For example, doing it properly and making your life a boatload easier with Flurl

C#:
void DoSomething(string userValue_Valiable, int[] numberValue, string snameValkue, string apiValue, string messageVarukavke){

 var person = await "https://api.com" 
  .AppendPathSegment("person")    
  .SetQueryParams(new { a = 1, b = 2 })    
  .WithOAuthBearerToken("token")   
  .PostJsonAsync(new{
    userName = userValue_Valiable,
    numbers  = numberValue,
    userSender = snameValkue,
    apiKey = apiValue,
    msg = messageVarukavke
  })
  .ReceiveJson<Person>();

That will not only make the json, it will also call the api and handle the httpclient properly, and deser the response into a usable object for you
 
Back
Top Bottom