Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOWTO store a para in the string?

Status
Not open for further replies.

133tcamel

Programmer
Sep 22, 2002
137
IN
Hi,
does somebody know of any easy idea to store a formatted paragraph in the string value.. I mean like how we have a HERE document in unix (or perl)..

some Delphi way to do this:

Code:
var
q: string;
s: string;
begin
     s := 'some text';

     q := '
     This is line 1
     This is line 2 with text "s"
     ';

end;

So that "q" contains all the newlines and the tabs and preferrably interpolates the string "s" too? Right now i'm doing it the hard way of adding each line with #13#9 etc..

Any help will be appreciated, ---
cheers!
san
pipe.gif
 
Hi San,

I know it seems like a simple question but you've actually opened up a "can'o'worms".

Firstly, what defines a paragraph? Common usage, that is what we visually know as a paragraph, comes from our usage of manual typewriters. In this case, a paragraph referred to a block of text that was visually separated from the next block of text by at least one blank line.

When this was translated to the computer environment, the "typewriter" actions were assigned to control characters.

#13 - CARRIAGE RETURN ($0C) is what happens when you push the typewriter carriage (carrying the paper) back to the left hand margin.

#10 - LINE FEED ($0A) is what happens when you pushed the silver handle fully down forcing the paper to roll up one line.

In these simple terms, a paragraph breaks occurs when two CR control characters follow one another. Note, there are other ways of achieving this effect but we need not go into that.

However, every Word Processing and Desktop Publishing package implements its own definitions of what constitutes CR/LF.

Therefore providing paragraph formatting actually requires you to know what program is going to interpret your control characters.

Anyway I won't comment any further on that here but I would like to comment on your code example as written. For any simple text editor, your variable 'q' would be printed out as follows:

This is line 1 This is line 2 with text "s"

As you see this prints all on one line without the contents of 's' being printed. What your code needs to look like is as follows:

q:='This is line 1'+#13#10+'This is line 2 with text '+s;

Hope this helps.

Roger Fedyk
 
thanks for the reply roger!:)

Like during the design time when you drop a "Memo" box and use the "..." on the lines property (Object Inspector) to invoke the code editor and paste the text into it (you can paste text with tabs, newlines, etc without the need to quote, escape or convert anything (it can't interpolate variables though) ) and then access the same using Memo1.Text (with the text intact)

Need to know if there is something similar for the runtime too (D5). So I can just paste my text documents and store them in a string as-it-is.
Right now I'm also doing the same thing as
'This is line 1'#13#9'This is line 2' but its a problem with bigger text and the text looses readability too.

I guess i can dynamically "load-from-file" but other options will be better, I guess. ---
cheers!
san
pipe.gif
 
Try using variables to make it easier to read eg,

NL:Char;
q:String;
s:string;

NL := chr(13);
s := 'some text';
q := Concat('This is line 1',NL,'This is line 2 with text ',s;

You could define NL as a constant.
 
If you define NL as a constant, be sure to declare it appropriately for your OS. Some word processors do funny things if the line break does not have the two characters typically used under DOS/Win, e.g. #13#10.

Alternatively, you could simply declare it as "\n" and let the OS handle the conversion appropriately.

Just a thought...

-- Lance
 
great advice.. thanks a lot guys!

I guess you people may know of this, but still one other thing worth sharing here (that I found out by hit and trial) yesterday is, when you paste a block of text in the lines property of Memo Box using the object inspector and then view the whole form as Text (Text DFM), Delphi has the neccessary translations made to the text pasted into it for the "memo1.lines", I mean with all special characters like tabs, quotes, etc converted to their equivalent representation, so i guess i can just copy that and paste it into my pas file from the DFM..

thanks for the all the help once again ---
cheers!
san
pipe.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top