Hello, I am trying to pass this string from back-end C# to front end, and want to render it as HTML, when passing this string to the front-end, it doesn't look proper, here is my current C# code.
var str="<span href='/ShowQuote/" +QID+ "?NewTab=True' style='margin: 10px; text-decoration:none' class='ShowQuoteClick'>Fetched one quote for " + MainCompanyName + " with Id-" + Id + "</span>";
in the above code QID
, MainCompanyName
& Id
is C# variables, so how can I insert variable values in the middle of a string properly, using C#?
Here are the ways to insert C# variable in between a string:
var str="<span href='/ShowQuote/{0}?NewTab=True' style='margin: 10px; text-decoration:none' class='ShowQuoteClick'>Fetched one quote for {1} with Id-{2}</span>";
var newStr=string.Format(str, QID,MainCompanyName,Id);?
var QID= "1";
var MainCompanyName = "Test";
var Id="11";
var str = $"<span href='/ShowQuote/{QID}?NewTab=True' style='margin: 10px; text-decoration:none' class='ShowQuoteClick'>Fetched one quote for {MainCompanyName} with Id-{Id}</span>";
You can check the detailed article on C# String Interpolation here or about C# Strings
Basically, you can format strings using String.Format
as sample is provided in above answer.
string name = "Vikas";
string developer = ".NET Developer";
Console.WriteLine(String.Format("Hi my name is {0}, working as {1}",
name, developer));
Hope it helps.
To insert a variable in string, simple use below code
string FirstValue = "value1";
string SecondValue = "value2";
Console. WriteLine("{0} and {1}", FirstValue, SecondValue);
You can also use string.replace
if you want to insert it in between
var sample = "My String #replace# Words";
var result = sample.Replace("#replace#", Var1);
That's it.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly