How to use `format` to repeat a character

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 11 09:07:04 PDT 2016


On Mon, Jul 11, 2016 at 02:53:24PM +0430, Bahman Movaqar via Digitalmars-d-learn wrote:
> On 07/11/2016 02:44 PM, ketmar wrote:
[...]
> > the fact that format can insert spaces. it is like: "ok, it can do
> > spaces. i bet there should be some way to use any character instead
> > of space. after all, the implementation would be the same, right?!"
> > and then... oops.
> 
> That's my story.
> 
> Thanks people for your help.

Here's a cheating way of doing it:

	import std.stdio, std.range;
	writefln("%.5s", repeat('-'));

It's cheating because the actual repeat is created by repeat(), but we
(ab)use the fact that the precision flag is treated as "maximum number
of characters" in the %s specifier to be able to control the length of
the repeat from the format string.


T

-- 
INTEL = Only half of "intelligence".


More information about the Digitalmars-d-learn mailing list