Is there a way to assign a multiline string value in Delphi without having to quote each line?
Edit (the specific problem): I have some SQL queries which I want to test outside Delphi. When copying the queries it is a bit of overhead to add and replace quotes every time.
Here’s code for an application you can add to the IDE’s Tools menu that might help. It was posted a while back to one of the CodeGear newsgroups by TeamB member Peter Below:
program ClipToStringConst; // Remove the dot from the line below for a console app, // per Rob Kennedy's comment. It works fine without being // a console app. {.$APPTYPE CONSOLE} uses Windows, Classes, Sysutils, APIClipboard; const cIndent = ' '; // 2 spaces cSingleQuote = ''''; EndChar : array [Boolean] of Char = ('+',';'); procedure Process; var SL: TStringlist; i, max: Integer; begin if ClipboardHasFormat( CF_TEXT ) then begin SL := TStringlist.Create; try SL.Text := ClipboardAsString; max := SL.count-1; for i:= 0 to max do SL[i] := cIndent + AnsiQuotedStr( TrimRight(SL[i])+#32, cSingleQuote ) + EndChar[i = max]; StringToClipboard( SL.Text ); finally SL.Free; end; { Finally } end; end; begin try Process; except on E: Exception do ShowException( E, ExceptAddr ); end; end.Just select the text in the SQL management tool after you’ve tested it and copy it to the clipboard. Switch to the Delphi Code Editor, place the insertion point where you want the constant text to appear, choose ‘Clipboard To Const’ or whatever you called it from the Tools menu, and then Ctrl+V to paste it into the editor.
It’s a pretty handy little tool. You can also modify it to work the opposite way (ConstantToClipboard) to remove the source formatting and revert back to raw SQL, although I haven’t bothered to do so yet.
EDIT: Missed a unit (APIClipboard). This needs to be a separate unit, obviously. Again, thanks to Peter Below:
Sample use:
Prepare the text in your SQL editor, text editor, or whatever:
Select all of the text, and copy to the clipboard using Ctrl+C.
Switch to the IDE’s Code Editor, run the ClipboardToStringConst application (using the Tools menu item you added, or whatever other means you want). Place the editor’s cursor (insertion point) where you want the constant text to appear, and press Ctrl+V to paste in the text.
The result: