Tell me if:
this is common knowledge
there's an easier way
why it's bad.
We've all gone through the fun of creating strings in script. If you have a complex SQL statement with functions and comparison operators, it looks gnarly enough in a query editor. Then to assign it to a string variable in script, you have to deal with special chars like quote and doublequote. And since &ersands and +plus signs are both string concatenators, they might cause problems. And to make line breaks, maybe for msgbox output, we use vbCrLf. We can tweek the string and check it with debug.print then see if it "works".
I've made plenty of code like newstring = "string" & vbCrLf & ""quoted string"" & "whatever".
There's plenty of tricks:
Double up every quote/doublequote or use chr(34).
Use find/replace in a text or code editor.
Use the Replace() function.
But if you are scripting on a web page, you can use an element (tag) to hold and assign a string exactly as is.
The example uses a <comment> tag because it naturally doesn't render, but you can use other tags (like <p>) and set the style="display:none".
tagstr = commInput.innerHTML
divOutput.innerText = tagstr
<comment id="commInput">'a " bc
de ; f</comment>
<button onclick="stringfromtag()">Fill DIV with String</button>
Between the start and end tags of the comment element is a string 17 chars long, including a tab, a CrLf, and 3 spaces in a row. Just make sure the start/end tags are together on the same line:
and anything you paste between them will be preserved, including all the whitespace and even html tags.
I've only tried this with notepad and my browsers; if you open/edit/save web pages with special editors, there's no telling what they might do.
I did a little searching, but didn't find anyone employing this method.
What do you think?