www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - text wrap

reply Joel <joelcnz gmail.com> writes:
What is simple way to wrap a string into strings?

Eg from:
I went for a walk and fell down a hole.

To (6 max width):
I went
for a
walk
and
fell
down a
hole.
Sep 19 2015
next sibling parent reply Doxin <lieuwemo gmail.com> writes:
On Sunday, 20 September 2015 at 00:22:17 UTC, Joel wrote:
 What is simple way to wrap a string into strings?

 Eg from:
 I went for a walk and fell down a hole.

 To (6 max width):
 I went
 for a
 walk
 and
 fell
 down a
 hole.
a common method works as follows. first you split your string into chunks where newlines might appear. e.g. after every space. then you keep track of a "line buffer", you keep adding chunks to the line buffer until it's too wide, then you remove one chunk and output the buffer as a line. rinse and repeat. I'll get to work on some example code.
Sep 19 2015
parent reply Doxin <lieuwemo gmail.com> writes:
On Sunday, 20 September 2015 at 00:28:23 UTC, Doxin wrote:
 I'll get to work on some example code.
here you go: http://dpaste.dzfl.pl/e6e715c54c1b do mind that this code has a couple issues, for example handing it a word longer than the break width will make it loop infinitely. word wrap is a surprisingly hard problem, but I hope I gave you a good enough starting point.
Sep 19 2015
parent Joel <joelcnz gmail.com> writes:
On Sunday, 20 September 2015 at 00:48:39 UTC, Doxin wrote:
 On Sunday, 20 September 2015 at 00:28:23 UTC, Doxin wrote:
 I'll get to work on some example code.
here you go: http://dpaste.dzfl.pl/e6e715c54c1b do mind that this code has a couple issues, for example handing it a word longer than the break width will make it loop infinitely. word wrap is a surprisingly hard problem, but I hope I gave you a good enough starting point.
Thanks Doxin. I going with the Phobos one, though. I've added your code to my large amount to of small programs.
Sep 19 2015
prev sibling next sibling parent Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
On Sunday, 20 September 2015 at 00:22:17 UTC, Joel wrote:
 What is simple way to wrap a string into strings?

 Eg from:
 I went for a walk and fell down a hole.

 To (6 max width):
 I went
 for a
 walk
 and
 fell
 down a
 hole.
http://dlang.org/phobos/std_string.html#.wrap
Sep 19 2015
prev sibling parent Joel <joelcnz gmail.com> writes:
On Sunday, 20 September 2015 at 00:22:17 UTC, Joel wrote:
 What is simple way to wrap a string into strings?

 Eg from:
 I went for a walk and fell down a hole.

 To (6 max width):
 I went
 for a
 walk
 and
 fell
 down a
 hole.
Actually, I did a search and found this. import std.string.wrap;
Sep 19 2015