-
-
Notifications
You must be signed in to change notification settings - Fork 105
v2 Lists
axunonb edited this page Feb 6, 2022
·
1 revision
The ListFormatter
repeats the contents of any IEnumerable
, e.g. an Array.
var items = new[] { "one", "two", "three" };
// important: do not use only "items" as a positional parameter, either put it inside another array or pass it as "arg0: items"
var result = Smart.Format("{0:list:{}|, |, and }", new object[] { items });
// Outputs: "one, two, and three"
{ IEnumerable : alias : template | spacer | finalSpacer }
IEnumerable | alias | template | spacer | finalSpacer |
---|---|---|---|---|
Any IEnumerable
|
"list" or "l" or implicit | required | required | optional |
- template will be repeated 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.
var array1 = new[] { "Pepsi", "Coke", "Mt. Dew", "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 = "{Results:list:{:list:|, |, and }|; |; }";
Console.WriteLine(SmartFormat.Smart.Format(namedFormatString, new {Results = arrayOfArrays}));
//outputs:
//Pepsi, Coke, Mt. Dew, and water; pizza, and hamburger; fries, chips, and pretzels; carrots, corn, and green beans
- 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