-
-
Notifications
You must be signed in to change notification settings - Fork 105
Choose _ ChooseFormatter
axunonb edited this page Oct 2, 2022
·
11 revisions
The ChooseFormatter
searches a value in a list of options, and outputs the corresponding value from a list of outputs.
{ Any Value : choose(1|2|n) : output 1 | output 2 | output n | default }
Value | formatter name | (choices) | outputs | default |
---|---|---|---|---|
Any value | "choose" | list of choices | list of outputs | (optional) output if nothing matched |
- There is no limitation for the number of choices and corresponding outputs
- The value can be anything. It will be converted to string (using
ToString
), and then matched against the choices. This works great for numbers, booleans, strings, enums, and most simple types. - outputs may include nested placeholders
- Comparing
bool
andnull
objects is not case-sensitive
string Name
: default ischoose
The name to use a named formatter
char SplitChar
: default is'|'
CaseSensitivityType CaseSensitivity
for choices: default isSmartSettings.CaseSensitivity
, unless overwritten
Smart.Format("{0:choose(1|2|3):one|two|three|other}", 1);
// outputs: "one"
Smart.Format("{0:choose(1|2|3):one|two|three|other}", 9);
// outputs: "other"
Smart.Format("{0:choose(4.0|4.1|4.2):dot zero|dot one|dot two|other}", 4.1M);
// outputs: "dot one"
Smart.Format("{0:choose(True|False):yes|no}", true);
Smart.Format("{0:choose(true|false):yes|no}", true);
// both output: "yes"
enum Gender { Female, Male, Unknown };
Smart.Format("{0:choose(Male|Female):his|her|their}", Gender.Female);
// outputs: "her"
Smart.Format("{0:choose(Male|Female):his|her|their}", Gender.None);
// outputs: "their"
Smart.Format("{0:choose(null): N/A|{}}", default(object));
Smart.Format("{0:choose(NULL): N/A|{}}", default(object));
// both outputs: "N/A"
Smart.Format("{0:choose(null|):null|empty|{}}", default(string?)))
// outputs: "null"
Smart.Format("{0:choose(null|):null|empty|{}}", ""))
// outputs: "empty"
Smart.Format("{0:choose(null|):null|empty|{}}", "something else"))
// "something else"
Smart.Format("{0:choose(null): N/A|{}}", 1234);
// outputs: "1234"
var input = null;
var arg = new {
Input = input,
Examples = new[] { "good", "bad" }
};
Smart.Format("{Input:choose(null|):Must be a word like {Examples:list:'{}'|, }|'' is not a word|{}}", arg);
// Outputs, depending on input variable:
null => "Must be a word like 'good', 'bad'"
"" => "'' is not a word"
"good" => "good"
"bad" => "bad"
Change the split char from '|'
to ','
(comma), so we can use '|'
for the output.
String comparisons are case-insensitive.
var chooseFmt = Smart.Default.GetFormatterExtension<ChooseFormatter>()!;
chooseFmt.SplitChar = ',';
chooseFmt.CaseSensitivity = CaseSensitivityType.CaseInsensitive;
Smart.Format("{0:choose(one,two,three):|1|,|2|,|3|,|??|}", "TWO");
// outputs: "|2|"
- 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