Re: create fixed length string of characters

2024-08-17 Thread IchorDev via Digitalmars-d-learn
On Saturday, 17 August 2024 at 06:03:09 UTC, Bruce wrote: Is there anything wrong with using the char[60] directly? In this case it’s fine to.

Re: create fixed length string of characters

2024-08-16 Thread Bruce via Digitalmars-d-learn
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: ```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(); This seems to work without having to make a string dup. I just w

Re: create fixed length string of characters

2024-08-16 Thread Renato Athaydes via Digitalmars-d-learn
On Friday, 16 August 2024 at 18:00:25 UTC, Renato Athaydes wrote: On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming

Re: create fixed length string of characters

2024-08-16 Thread Renato Athaydes via Digitalmars-d-learn
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```

Re: create fixed length string of characters

2024-08-16 Thread IchorDev via Digitalmars-d-learn
On Friday, 16 August 2024 at 15:58:31 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming y

Re: create fixed length string of characters

2024-08-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, August 16, 2024 10:37:45 AM MDT Nick Treleaven via Digitalmars-d- learn wrote: > On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote: > > Whether the result of dup is then able to be implicitly > > converted to immutable based on whether the operation is pure > > depends on

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 August 2024 at 16:30:09 UTC, Jonathan M Davis wrote: Well, you if you use dup, you're asking for a mutable array, whereas if you use idup, you're asking for an immutable array. Yes, `idup` may be needed e.g. for overloads varying on mutability. Whether the result of dup is then

Re: create fixed length string of characters

2024-08-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, August 16, 2024 9:58:31 AM MDT Nick Treleaven via Digitalmars-d- learn wrote: > On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: > > On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: > >> On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: > >> ```d > >> stri

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
On Friday, 16 August 2024 at 13:30:53 UTC, IchorDev wrote: On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ```

Re: create fixed length string of characters

2024-08-16 Thread IchorDev via Digitalmars-d-learn
On Friday, 16 August 2024 at 11:37:08 UTC, Nick Treleaven wrote: On Friday, 16 August 2024 at 06:15:18 UTC, Bruce wrote: ```d string s = a.dup; // copy to heap, assuming you need the data to escape (use a[] otherwise) s.writeln(); ``` I think you meant `idup`? `dup` will make it mutable.

Re: create fixed length string of characters

2024-08-16 Thread Nick Treleaven via Digitalmars-d-learn
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[] othe

Re: create fixed length string of characters

2024-08-15 Thread Olivier Pisano via Digitalmars-d-learn
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 You can use the repeat() function in std.range to create a range of N consecutive elements. If you need to assign this range to a string, you'll have