I have been reading about verbatim string literals and escape sequences. What I am curious about now is if it is possible to escape and say call a dynamic source from a database when using verbatim string literals?
For example how would one achieve something like below?
string jsFunc = @"
(function()
{
var image = 'Images/" + {sqlConn[2]} + "';
var img = document.getElementbyId('img1');
img.src = image;
}
);";
Thanks
You have 3 options:
string jsFunc = $@"
(function()
{{
var image = 'Images/{sqlConn[2]}';
var img = document.getElementbyId('img1');
img.src = image;
}}
);";
(Notice the $
before the @
and the removal of the string concatenation part, which turns this into an interpolated string. Also note that I had to double up the braces to avoid tripping up the underlying string.Format
.)
Or this:
string jsFunc = string.Format(@"
(function()
{{
var image = 'Images/{0}';
var img = document.getElementbyId('img1');
img.src = image;
}}
);", sqlConn[2]);
Which is just good'ol string.Format . Same here about doubling up the braces.
Or this:
string jsFunc = @"
(function()
{
var image = 'Images/" + sqlConn[2] + @"';
var img = document.getElementbyId('img1');
img.src = image;
}
);";
(Notice that I added a new @
before the rest of the string after the concatenation, and also removed the braces.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.