Embedded return chars in a string


I'm trying to embed carriage returns in a Delphi long string and then printing it out in the following manner (in a console app):

var
    aString : String;
begin
    aString := 'Hello' + Chr(13) + 'World';
    Writeln(aString);
end.

But instead of getting the word "Hello" in one line and "World" on another, I get some garbled text all on one line, e.g. "HeWorld".  Does writeln not interpret carriage returns within a text?  Or is 13 the wrong ASCII value for a carriage return?


Thanks,

Ap9
ap9Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rwilson032697Connect With a Mentor Commented:
You need to add CHR(10) as well.

You can also use #13 and #10 as shorthand notations.

Cheers,

Raymond.
0
 
RBertoraCommented:
Try this:

var
    aString : String;
begin
    aString := 'Hello' + #10#13 + 'World';
    Write(aString);
end.


Notice you use Write not writeln.

Rob ;-)
0
 
sburckCommented:
A couple of elaborations:  

1)  #13 is a carriage return, #10 is a line feed, which is what will separate them on two lines.

2)  Even shorterhand is:
    astring := 'Hello'#10#13'World';

3)  This works with both write and writeln, writeln appends a CR/LF at the end of the string as well.
0
 
ap9Author Commented:
Thanks to all that responded!  The missing linefeed was the trick!

I had written a test program that displayed the the ASCII value of key presses, and hitting ENTER made it display a "13", so I had thought that was all I needed.  I guess not.

Thanks again!


AP9
0
All Courses

From novice to tech pro — start learning today.