Thread overview
text wrap
Sep 20, 2015
Joel
Sep 20, 2015
Doxin
Sep 20, 2015
Doxin
Sep 20, 2015
Joel
Sep 20, 2015
Vladimir Panteleev
Sep 20, 2015
Joel
September 20, 2015
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.

September 20, 2015
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.
September 20, 2015
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
September 20, 2015
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;

September 20, 2015
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.

September 20, 2015
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.