Reputation: 339
Hi I have the following line:
var table = @"<table id=""table_id"" class=""display"">
which is building a table and continues on the next line but I'm just trying to append a string at the end of table_id :
var table = @"<table id=""table_id" + instance + """ class=""display"">
so the final output (if instance = 1234) should be:
<table id="table_id1234" class="display">
But I think the quotes are throwing it off. Any suggestions on how t achieve the last line?
Thanks
Upvotes: 3
Views: 4470
Reputation: 361
@ is used to escape double quotes from one string but in your example, you are actually concatenating three different strings, soyou should escape the third string as well like this:
var table = @"<table id=""table_id" + instance + @" "" class=""display"" >";
Alternatively, you could also use the StringBuilder class which is more memory efficient and might make your strings easier to read.
Upvotes: 0
Reputation: 41
I think the best idea and newest idea for this situation is $ sign before your text and with this sign you dont need to extra sign in your string
example
vat table = $"<table id='table_id{instance}' class='display'">
Upvotes: 0
Reputation: 1909
Try to use escape character for double quote(\"
) using this code:
var id = "1234";
var table = "<table id=\"table_id" + id + "\" class=\"display\">";
Here is an online tool for converting string to escape/unescape:
https://www.freeformatter.com/java-dotnet-escape.html
So you can copy the result and place your variables.
Upvotes: 1
Reputation: 24957
A string.Format
method placeholder is enough to concatenate instance
without cutting through quote signs ({0}
is the placeholder):
var table = string.Format(@"<table id=""table_id{0}"" class=""display"">", instance);
Or you can use escape sequence \"
for escaping quotes without string literal:
var table = "<table id=\"table_id" + instance + "\" class=\"display\">"
Result:
<table id="table_id1234" class="display">
Demo: .NET Fiddle
Upvotes: 7