Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Formatting for ordinary strings (not Stdout)

Moderators: larsivi kris

Posted: 03/10/07 19:06:16

Thanks for implementing C# style format strings. They're very very handy. I've read the tutorial here, and it's very straightforward to use the format strings for writing to Stdout.

But how do I use them to format ordinary strings in memory?

In C#, I'd just do:

String formatted = String.Format("This is the thing: {0}", thing);

There doesn't seem to be any way to do this right now (I'm using the Tango 0.95 beta).

Thanks!!

--benji

Author Message

Posted: 03/10/07 19:37:04 -- Modified: 03/10/07 19:37:38 by
larsivi

This functionality is in the tango.text.Layout class, using the convert or sprint methods. If you've already imported Stdout, you can access it by

char[] string = Stdout.layout.convert("{} etc", foo);

Layout you need to instantiate yourself with the char type you need.

Posted: 03/11/07 22:20:28 -- Modified: 03/11/07 22:21:47 by
BenjiSmith -- Modified 2 Times

Hmmmmm. I'm actually using the 0.95 beta (rather than working from SVN), and in this version, there is no tango.text.Layout class.

It looks like it's in the tango.text.convert.Format module, and that the object is called Format(T). I can get what I want with:

auto charFormatter = new Format!(char)();
char[] formatted = charFormatter("This is the thing: '{0}'", thing);

Some mention of this should really be in the manual, or in the tutorials somewhere. I had to dig through quite a bit of source code to find this.

Posted: 03/11/07 22:38:20

I added this new page to the wiki, explaining how to use string formatting without having to go through the Stdout or Stderr classes:

http://www.dsource.org/projects/tango/wiki/TutCSharpFormatterComments

Posted: 03/12/07 01:13:31

There have been a lot of changes since the last release as we fine-tune things in anticipation of 1.0. The next release is due out in about a week.

Posted: 03/12/07 06:00:26

Fantastic!! I'm really looking forward to it.

So far, I've really been enjoying using Tango in my projects. It's a great library.

Posted: 03/16/07 18:34:29

updated your additional comments to reflect beta2 changes

- Arrr! We'll all be walkin' the Plank! -