Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# appending string from variable inside double quotes

Tags:

c#

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

like image 274
Zee Fer Avatar asked Jun 10 '26 21:06

Zee Fer


1 Answers

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

like image 182
Tetsuya Yamamoto Avatar answered Jun 13 '26 12:06

Tetsuya Yamamoto