On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote:
Is there an easy way to create a 60 character string in D?
Like in Python...
ul = '-'*60

2 ways:

```d
// use a fixed array:
immutable char[60] a = '-';
string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise)
s.writeln();

// allocate mutable array on heap
char[] b = new char[60];
b[] = '-';
// if b is unique and no longer used
s = cast(string)b; // not @safe
s.writeln();
```

`a` is initialized from a single element which is copied over each of its elements. `b` uses an array operation to assign a single element to each of its elements.

I think the 2nd way may be more efficient, but it requires an unsafe cast.

Reply via email to