digitalmars.D.learn - How to use `format` to repeat a character
- Bahman Movaqar (10/10) Jul 11 2016 I'm sure I'm missing something very simple but how can I create a string
- =?UTF-8?Q?Ali_=c3=87ehreli?= (21/26) Jul 11 2016 What makes you expect that format should have that feature? :) Perhaps
- ag0aep6g (6/10) Jul 11 2016 An alternative without those scary modulo and ternary operators, just
- ketmar (6/7) Jul 11 2016 the fact that format can insert spaces. it is like: "ok, it can
- Bahman Movaqar (6/12) Jul 11 2016 I somehow recalled I could do that in C and then there was the "minimum
- Mike Parker (3/9) Jul 11 2016 You can do it in D with custom format specifiers. See:
- Bahman Movaqar (4/7) Jul 11 2016 Thanks for the pointer. I'll keep that in mind.
- H. S. Teoh via Digitalmars-d-learn (12/21) Jul 11 2016 Here's a cheating way of doing it:
- Meta (6/12) Jul 11 2016 There's at least one way to do this, by using position arguments
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
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
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
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
On 07/11/2016 02:44 PM, ketmar wrote:On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote: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 :-)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.That's my story. Thanks people for your help.
Jul 11 2016
On Monday, 11 July 2016 at 10:23:24 UTC, Bahman Movaqar wrote:On 07/11/2016 02:44 PM, ketmar wrote:You can do it in D with custom format specifiers. See: https://wiki.dlang.org/Defining_custom_print_format_specifiersOn Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote: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 :-)What makes you expect that format should have that feature? :)
Jul 11 2016
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_specifiersThanks for the pointer. I'll keep that in mind. -- Bahman
Jul 11 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:[...]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".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
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