www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use `format` to repeat a character

reply Bahman Movaqar <Bahma BahmanM.com> writes:
I'm sure I'm missing something very simple but how can I create a string
like "----" using `format`?
I check the docs on `format` and tried many variations including
`format("%.*c\n", 4, '-')` but got nowhere.

I'd appreciate any hint/help on this.

-- 
Bahman Movaqar

http://BahmanM.com - https://twitter.com/bahman__m
https://github.com/bahmanm - https://gist.github.com/bahmanm
PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com)
Jul 11 2016
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 07/11/2016 02:02 AM, Bahman Movaqar wrote:
 I'm sure I'm missing something very simple but how can I create a string
 like "----" using `format`?
You can't.
 I check the docs on `format` and tried many variations including
 `format("%.*c\n", 4, '-')` but got nowhere.
What makes you expect that format should have that feature? :) Perhaps you're familiar with another language's standard library that does that?
 I'd appreciate any hint/help on this.
There are several ways of repeating characters and range elements in general: void main() { // 'replicate' copies an array (which strings are) eagerly import std.array : replicate; assert("-".replicate(3) == "---"); // 'repeat' repeats lazily import std.range : repeat; import std.algorithm : equal; assert('-'.repeat(3).equal("---")); // Another one that combines multiple range algorithms import std.range : iota; import std.algorithm : map; assert(7.iota.map!(i => i % 2 ? '=' : '-').equal("-=-=-=-")); // etc. } Ali
Jul 11 2016
next sibling parent ag0aep6g <anonymous example.com> writes:
On 07/11/2016 11:31 AM, Ali Çehreli wrote:
      // Another one that combines multiple range algorithms
      import std.range : iota;
      import std.algorithm : map;
      assert(7.iota.map!(i => i % 2 ? '=' : '-').equal("-=-=-=-"));
An alternative without those scary modulo and ternary operators, just because I think it's cute: import std.range: repeat, roundRobin, take; import std.algorithm: equal; assert(roundRobin(repeat('-'), repeat('=')).take(7).equal("-=-=-=-"));
Jul 11 2016
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:
 What makes you expect that format should have that feature? :)
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. probably this worth a ER.
Jul 11 2016
parent reply Bahman Movaqar <Bahma BahmanM.com> writes:
On 07/11/2016 02:44 PM, ketmar wrote:
 On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:
 What makes you expect that format should have that feature? :)
I somehow recalled I could do that in C and then there was the "minimum field width" in the docs, so I thought it's possible I'm just not understanding the docs clearly :-)
 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.
Jul 11 2016
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 11 July 2016 at 10:23:24 UTC, Bahman Movaqar wrote:
 On 07/11/2016 02:44 PM, ketmar wrote:
 On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:
 What makes you expect that format should have that feature? :)
I somehow recalled I could do that in C and then there was the "minimum field width" in the docs, so I thought it's possible I'm just not understanding the docs clearly :-)
You can do it in D with custom format specifiers. See: https://wiki.dlang.org/Defining_custom_print_format_specifiers
Jul 11 2016
parent Bahman Movaqar <Bahma BahmanM.com> writes:
On 07/11/2016 03:02 PM, Mike Parker wrote:
 You can do it in D with custom format specifiers. See:
 
 https://wiki.dlang.org/Defining_custom_print_format_specifiers
Thanks for the pointer. I'll keep that in mind. -- Bahman
Jul 11 2016
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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".
Jul 11 2016
prev sibling parent Meta <jared771 gmail.com> writes:
On Monday, 11 July 2016 at 09:02:12 UTC, Bahman Movaqar wrote:
 I'm sure I'm missing something very simple but how can I create 
 a string
 like "----" using `format`?
 I check the docs on `format` and tried many variations including
 `format("%.*c\n", 4, '-')` but got nowhere.

 I'd appreciate any hint/help on this.
There's at least one way to do this, by using position arguments and repeating it the desired number of times in the format string. format("%1$s%1$s%1$s%1$s\n", '-') But as you can see this is pretty ugly and can easily introduce bugs in your format string.
Jul 11 2016