-
-
Notifications
You must be signed in to change notification settings - Fork 107
Lists _ ListFormatter
axunonb edited this page Mar 3, 2022
·
8 revisions
The ListFormatter
enumerates the content of an IEnumerable
, e.g. an Array
. The data source must be an IList
.
The ListFormatter
implements ISource
and IFormatter
.
{ IEnumerable : formatter-name : template | spacer | finalSpacer }
IEnumerable | formatter name | template | spacer | finalSpacer |
---|---|---|---|---|
Any IEnumerable
|
"list" or implicit | required | required | optional |
- template will be used for each item
- spacer will be added after each item except the last
- finalSpacer if supplied, will replace the very final spacer
Note: The spacers may also in include character literals. So e.g. instead of a comma you could also use \n for listing each item on a new line.
char SplitChar
: default is'|'
The character used to split the option text literals
var items = new[] { "one", "two", "three" };
// Important: You cannot use "items" as an indexed parameter directly,
// as it would be used as params with 3 args.
// So we have to cast
var result = Smart.Format("{0:list:{}|, |, and }", (IList) items);
// Outputs: "one, two, and three"
var array1 = new[] { "Pepsi", "Coke", "beer", "water" };
var array2 = new[] { "pizza", "hamburger" };
var array3 = new[] { "fries", "chips", "pretzels" };
var array4 = new[] { "carrots", "corn", "green beans" };
var arrayOfArrays = new[] { array1, array2, array3, array4 };
var namedFormatString = "{Food:list:{:list:|, |, and }|;\n|;\n}";
Smart.Format(namedFormatString, new {Food = arrayOfArrays});
/* outputs:
Pepsi, Coke, beer, and water;
pizza, and hamburger;
fries, chips, and pretzels;
carrots, corn, and green beans
*/
Smart.Format("{TheList?:list:{}|, |, and}", new { TheList = default(object)});
// outputs: ""
var letters = "ABC".ToCharArray();
var words = "One|Two|Three".Split('|');
// works with indexed and named placeholders
Smart.Format("{0:list:{} = {1[Index]}|, }", letters, words);
// outputs: "A = One, B = Two, C = Three"
var data = new { Words = "One|Two|Three".Split('|') };
Smart.Format("Second word: {Words[1]}", data);
// outputs: "Second word: Two"
Change the split char from '|'
to '\t'
,
so we can use '|'
for the output
Smart.Default.GetFormatterExtension<ListFormatter>()!.SplitChar = '\t';
var items = new[] { "one", "two", "three" };
Smart.Format("{0:list:{}\t|\t|}", (IList) items);
// outputs: "one|two|three"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License