digitalmars.D.learn - a function like writeln that returns a string rather than writes to a
- dan (20/20) May 01 2020 I'm looking for a function something like writeln or write, but
- H. S. Teoh (7/9) May 01 2020 [...]
- dan (10/19) May 01 2020 Thanks HS!
- Steven Schveighoffer (4/26) May 01 2020 import std.conv: text;
- dan (5/30) May 01 2020 Awesome, thanks Steve. That's perfect. So the function i was
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/27) May 02 2020 If you can live with a mildly awkward way of passing it, format() can
- dan (8/44) May 02 2020 Thanks Ali.
I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. So i would like something like: import std.stdio; import std.conv; string write_to_string(T...)(T values ) { string s; foreach ( value; values ) s ~= to!string( value ); return s; } But because this is such a standard type of thing to do, i'd like to use whatever the standard function is for doing it, if there is one. So . . . is there one? Like maybe some way to dress a string up as a file and pass it through the usual write/writeln apparatus? My only real requirement is that it be something really easy to do. Thanks in advance for any pointers. dan
May 01 2020
On Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn wrote:I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string.[...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T -- Once bitten, twice cry...
May 01 2020
On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:On Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn wrote:Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. Anyhow, though, thanks for point out format. danI'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string.[...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T
May 01 2020
On 5/1/20 10:40 PM, dan wrote:On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:import std.conv: text; string str = text(obj1, " ", obj2, " ", obj3); -SteveOn Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn wrote:Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does.I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string.[...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T
May 01 2020
On Saturday, 2 May 2020 at 02:49:04 UTC, Steven Schveighoffer wrote:On 5/1/20 10:40 PM, dan wrote:Awesome, thanks Steve. That's perfect. So the function i was looking for was text (or, i guess, std.conv.text). danOn Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:import std.conv: text; string str = text(obj1, " ", obj2, " ", obj3); -SteveOn Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn wrote:Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does.[...][...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T
May 01 2020
On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:If you can live with a mildly awkward way of passing it, format() can take the format string at compile time as well: string str = format!"%s %s %s"(obj1, obj2, obj3); You get a compilation error if format specifications don't match the arguments. (There are bug reports about that check but it mostly works great.) AliOn Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn wrote:Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does.I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string.[...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T
May 02 2020
On Saturday, 2 May 2020 at 10:36:47 UTC, Ali Çehreli wrote:On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote:Thanks Ali. That's also a good point, and would remove one of my qualms about all of the %s reps. So if for any reason i cannot use the text function (or if i want to double check on the types of the objects) this would be a good thing to use. danDigitalmars-d-learnOn Sat, May 02, 2020 at 02:22:42AM +0000, dan viabutwrote:I'm looking for a function something like writeln or write,returns theinstead of writing to stdout, it writes to a string andconversion forThanks HS! That looks like a good move, if format will do the stringstring.[...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); Tme. But one thing that would be troublesome is that i would haveto makesure to count up the %s so that they match the number ofarguments. Iwould like to do without that, just like writeln does.If you can live with a mildly awkward way of passing it, format() can take the format string at compile time as well: string str = format!"%s %s %s"(obj1, obj2, obj3); You get a compilation error if format specifications don't match the arguments. (There are bug reports about that check but it mostly works great.) Ali
May 02 2020