www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiply a string?

reply Andy Valencia <dont spam.me> writes:
In logging output, I often want indentation.  In Python, the 
output is built like:

```
print(" " * ddepth, "data at this level: ", datum)
```

Which is to say, build a string by concatenating " " ddepth 
times.  Is there a comparable idiom in dlang or Phobos?

Thanks!
Andy
Jun 07
next sibling parent reply Sergey <kornburn yandex.ru> writes:
On Saturday, 7 June 2025 at 16:13:06 UTC, Andy Valencia wrote:
 Which is to say, build a string by concatenating " " ddepth 
 times.  Is there a comparable idiom in dlang or Phobos?

 Thanks!
 Andy
I think there are many ways to do it The simplest that comes to my mind: ```d void main() { string error_message = "D's desing is far from perfect"; writeln(" ".repeat(5).join, "[INFO]:", Clock.currTime(), " - ", error_message); } ```
Jun 07
next sibling parent Andy Valencia <dont spam.me> writes:
On Saturday, 7 June 2025 at 16:42:51 UTC, Sergey wrote:
 I think there are many ways to do it
 The simplest that comes to my mind:
Thanks! repeat() it is (though I just wrote on which avoids the interim representation, so no join() needed). Andy
Jun 07
prev sibling parent reply Neto <netorib94 gmail.com> writes:
On Saturday, 7 June 2025 at 16:42:51 UTC, Sergey wrote:
 On Saturday, 7 June 2025 at 16:13:06 UTC, Andy Valencia wrote:
 Which is to say, build a string by concatenating " " ddepth 
 times.  Is there a comparable idiom in dlang or Phobos?

 Thanks!
 Andy
I think there are many ways to do it The simplest that comes to my mind: ```d void main() { string error_message = "D's desing is far from perfect"; writeln(" ".repeat(5).join, "[INFO]:", Clock.currTime(), " - ", error_message); } ```
is `repeat` from a native module? if so, which one?
Jun 21
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Sat, Jun 21, 2025 at 04:18:37PM +0000, Neto via Digitalmars-d-learn wrote:
 On Saturday, 7 June 2025 at 16:42:51 UTC, Sergey wrote:
[...]
 ```d
 void main() {
     string error_message = "D's desing is far from perfect";
     writeln(" ".repeat(5).join, "[INFO]:", Clock.currTime(), " - ",
 error_message);
 }
 ```
is `repeat` from a native module? if so, which one?
std.range T -- Elegant or ugly code as well as fine or rude sentences have something in common: they don't depend on the language. -- Luca De Vitis
Jun 21
parent Neto <netorib94 gmail.com> writes:
On Saturday, 21 June 2025 at 16:23:42 UTC, H. S. Teoh wrote:
 On Sat, Jun 21, 2025 at 04:18:37PM +0000, Neto via 
 Digitalmars-d-learn wrote:
 On Saturday, 7 June 2025 at 16:42:51 UTC, Sergey wrote:
[...]
 ```d
 void main() {
     string error_message = "D's desing is far from perfect";
     writeln(" ".repeat(5).join, "[INFO]:", Clock.currTime(), 
 " - ",
 error_message);
 }
 ```
is `repeat` from a native module? if so, which one?
std.range T
thanks
Jun 21
prev sibling parent reply Jabba Laci <jabba.laci gmail.com> writes:
On Saturday, 7 June 2025 at 16:13:06 UTC, Andy Valencia wrote:
 Which is to say, build a string by concatenating " " ddepth 
 times.  Is there a comparable idiom in dlang or Phobos?
I use this: ```d import std.stdio; import std.array; // from here void main() { writeln("-".replicate(5)); // must be a string } ```
Jun 21
parent Neto <netorib94 gmail.com> writes:
On Saturday, 21 June 2025 at 12:43:31 UTC, Jabba Laci wrote:
 On Saturday, 7 June 2025 at 16:13:06 UTC, Andy Valencia wrote:
 Which is to say, build a string by concatenating " " ddepth 
 times.  Is there a comparable idiom in dlang or Phobos?
I use this: ```d import std.stdio; import std.array; // from here void main() { writeln("-".replicate(5)); // must be a string } ```
very nice solution
Jun 21