|
- Author:
- kris (IP: 68.122.70.98)
- Timestamp:
- 03/16/07 18:33:02 (17 years ago)
- Comment:
updated to beta2
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TutCSharpFormatterComments
v1 |
v2 |
|
3 | 3 | The [wiki:TutCSharpFormatter existing CSharp Formatter tutorial] doesn't make it obvious that string formatting is available in any other context than the console. But it is!! Anywhere you use strings, you can also create formatted strings!! |
---|
4 | 4 | |
---|
5 | | The interesting code is in the tango.text.convert.Format module, in a templated class called Format(T). You can instantiate the template with char, dchar, or wchar types, and use the !OpCall syntax to format your strings, like this: |
---|
6 | | |
---|
| 5 | The interesting code is in the tango.text.convert.Layout module, in a templated class called Layout(T). You can instantiate the template with char, dchar, or wchar types, and use the !OpCall syntax to format your strings, like this: |
---|
7 | 6 | {{{ |
---|
8 | | import tango.text.convert.Format; |
---|
| 7 | import tango.text.convert.Layout; |
---|
9 | 8 | |
---|
10 | 9 | // ... |
---|
11 | 10 | |
---|
12 | | auto charFormatter = new Format!(char)(); |
---|
13 | | char[] formattedString = charFormatter("This is the thing: '{0}'", thing); |
---|
| 11 | auto charFormatter = new Layout!(char); |
---|
| 12 | char[] formattedString = charFormatter("This is the thing: '{}'", thing); |
---|
14 | 13 | |
---|
15 | | auto wcharFormatter = new Format!(wchar)(); |
---|
16 | | wchar[] formattedWString = wcharFormatter ("This is the thing: '{0}'", thing); |
---|
| 14 | auto wcharFormatter = new Layout!(wchar); |
---|
| 15 | wchar[] formattedWString = wcharFormatter ("This is the thing: '{}'", thing); |
---|
17 | 16 | |
---|
18 | | auto dcharFormatter = new Format!(dchar)(); |
---|
19 | | dchar[] formattedDString = dcharFormatter ("This is the thing: '{0}'", thing); |
---|
| 17 | auto dcharFormatter = new Layout!(dchar); |
---|
| 18 | dchar[] formattedDString = dcharFormatter ("This is the thing: '{}'", thing); |
---|
20 | 19 | }}} |
---|
| 20 | |
---|
| 21 | You might consider the sprint() method also, which avoids heap activity. Also, if you're already using Stdout or Stderr, it can be simpler to apply the Layout instance exposed there instead: |
---|
| 22 | {{{ |
---|
| 23 | char[256] out = void; |
---|
| 24 | auto content = Stdout.layout.sprint (out, "This is the thing: '{}'", thing); |
---|
| 25 | }}} |
---|
| 26 | |
---|
| 27 | One might also utilize the locale enabled extensions in a similar manner, by using tango.text.locale.Locale instead of Layout. Locale is a derivative of Layout, so all formatting methods are common with Locale adding support for more sophisticated formatting options along with culture-specific currency, time and date consideration: |
---|
| 28 | {{{ |
---|
| 29 | import tango.text.locale.Locale; |
---|
| 30 | |
---|
| 31 | auto layout = new Locale (Culture.getCulture ("fr-FR")); |
---|
| 32 | auto formattedString = layout ("This is the date: {}", DateTime.now); |
---|
| 33 | |
---|
| 34 | }}} |
---|
| 35 | |
---|
| 36 | It's also possible to make Stdout and Stderr locale-aware, by replacing the (shared) layout instance: |
---|
| 37 | {{{ |
---|
| 38 | Stdout.layout = new Locale (Culture.getCulture ("fr-FR")); |
---|
| 39 | }}} |
---|
| 40 | |
|
|
|
|
|
Copyright © 2006-2024 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic