String'n together combos isn't always as fun as doing them in a fighting videogame.Image may be NSFW.
Clik here to view.Image may be NSFW.
Clik here to view.
If you write script that has a lot String content mixed with variables, I'm sure you're doing a lot of heavy concatenation.
Building string combos with quotes and apostrophes sprinkled all over a script gets confusing. The way I like to keep this confusion to a minimum is to write my static markup separately. This allows me to make sure it shows up as intended. Then I concatenate my variables in all the places that need to have dynamic content.
Taking static markup or script and wrapping quotes around every line is a chore so I made this Concatenation Tool to help with that.
Currently I've only made this support languages I'm using mostly. If I have to use a new language I'd just add it in.
Below are some basic use case examples of where a concatenation tool can be useful.
JavaScript
Concatenating a content to add to a Jquery UI Dialog.
In this example I purposely used he "text()" method to show the XML content instead of "html()".
<script type="text/javascript"> $(document).ready(function(){ var ns = ""; ns += "<LIBRARY>"; ns += " <BOOK>"; ns += " <TITLE id=\"2356\">Charlotte's Web</TITLE>"; ns += " <AUTHOR>E. B. White</AUTHOR>"; ns += " <FAMOUS_SAYING>\"Always be on the lookout for the presence of wonder.\"</FAMOUS_SAYING>"; ns += " </BOOK>"; ns += " <BOOK>"; ns += " <TITLE id=\"95\">Scuffy the Tugboat</TITLE>"; ns += " <AUTHOR>Gertrude Crampton</AUTHOR>"; ns += " <FAMOUS_SAYING>\"Toot, tooot!\" cried the frightened tugboat.</FAMOUS_SAYING>"; ns += " </BOOK>"; ns += "</LIBRARY>"; $("#myDialog").dialog({width:400, height:400}); $("#myDialog p").text(ns); }); </script>
Image may be NSFW.
Clik here to view.
Python
Concatenating with qoutes
ns = "" ns += "<LIBRARY>" ns += " <BOOK>" ns += " <TITLE id=\"2356\">Charlotte's Web</TITLE>" ns += " <AUTHOR>E. B. White</AUTHOR>" ns += " <FAMOUS_SAYING>\"Always be on the lookout for the presence of wonder.\"</FAMOUS_SAYING>" ns += " </BOOK>" ns += " <BOOK>" ns += " <TITLE id=\"95\">Scuffy the Tugboat</TITLE>" ns += " <AUTHOR>Gertrude Crampton</AUTHOR>" ns += " <FAMOUS_SAYING>\"Toot, tooot!\" cried the frightened tugboat.</FAMOUS_SAYING>" ns += " </BOOK>" ns += "</LIBRARY>" print(ns)
Python concatenation with triple quotes to preserve the carriage returns
ns="""<LIBRARY> <BOOK> <TITLE id="2356">Charlotte's Web</TITLE> <AUTHOR>E. B. White</AUTHOR> <FAMOUS_SAYING>"Always be on the lookout for the presence of wonder."</FAMOUS_SAYING> </BOOK> <BOOK> <TITLE id="95">Scuffy the Tugboat</TITLE> <AUTHOR>Gertrude Crampton</AUTHOR> <FAMOUS_SAYING>"Toot, tooot!" cried the frightened tugboat.</FAMOUS_SAYING> </BOOK> </LIBRARY>""" print(ns)
Concatenation with regular quotes
Image may be NSFW.
Clik here to view.
Concatenation of blocks of text with triple quotes
Image may be NSFW.
Clik here to view.
JSFL
Concatenating a XUL markup string for building Flash IDE Panel with JSFL.
if(fl.documents.length == 0){ fl.createDocument("timeline"); } //------------------------------------------------------------ //An existing folder for the panel's source to be written to. //------------------------------------------------------------ var appFolderName="jsflUI_sample"; var date=new Date(); function writePanelSource() { var code = ''; code += '<overlay>'; code += ' <dialog id="app" title="Sample UI">'; code += ' <hbox>'; code += ' <spacer/>'; code += ' <button id="b0" label="Button"/>'; code += ' </hbox>'; code += ' <separator/>'; code += ' <vbox>'; code += ' <label control="f0" value="Field 1" align="left"/>'; code += ' <textbox id="f0" size="50" value="'+date.toString()+'"/>'; code += ' <label control="f1" value="Field 2:" align="left"/>'; code += ' <textbox id="f1" size="50" value=""/>'; code += ' </vbox>'; code += ' <checkbox id="cb0" label="Checkbox 1" checked="false" />'; code += ' <checkbox id="cb1" label="Checkbox 2" checked="false" />'; code += ' </dialog>'; code += '</overlay>'; FLfile.write(fl.configURI + "Commands/"+appFolderName+"/panelsource.xml", code); return code; } //---------------------------------------------------------------- //Writes the markup to "appFolderName" of Flash's "Commands folder //---------------------------------------------------------------- writePanelSource(); //------------------------------- //Open the Panel in Flash's IDE //------------------------------- fl.getDocumentDOM().xmlPanel(fl.configURI + "Commands/"+appFolderName+"/panelsource.xml");
Panel UI dialog from JSFL
Image may be NSFW.
Clik here to view.
SharePoint
Concatenating CAML querys.
function getListData() { var clientContext = new SP.ClientContext.get_current(); var web = clientContext.get_web(); var userInfoList = web.get_lists().getByTitle('user_list') var camlQuery = new SP.CamlQuery(); var caml = ''; caml += '<View>'; caml += ' <Query>'; caml += ' <OrderBy>'; caml += ' <FieldRef Name="Title" Ascending="False" />'; caml += ' </OrderBy>'; caml += ' </Query>'; caml += '</View>'; camlQuery.set_viewXml(caml); collListItem = userInfoList.getItems(camlQuery); clientContext.load(collListItem); var onQuerySucceeded=function(sender, args) { var oitem; var s=''; var n=0; var len=collListItem.get_count(); var results=[]; while(n < len) { oitem = collListItem.itemAt(n); s+="Title:"+oitem.get_item("Title")+"<br></br>"; n++; } $("#output").append(s); } var onQueryFailed=function(sender, args) { alert("Failed getting data.") } clientContext.executeQueryAsync ( Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed) ); }
Output of Title column from a SharePoint 2010 list.
Image may be NSFW.
Clik here to view.