-
-
Notifications
You must be signed in to change notification settings - Fork 107
v2 SubString
axunonb edited this page Feb 6, 2022
·
1 revision
The SubStringFormatter
lets you output parts of an input string.
var people = new List<object>
{new {Name = "Long John", City = "New York"}, new {Name = "Short Mary", City = "Massachusetts"},};
Smart.Format("{Name:substr(5)}", people.First());
// result: "John"
Smart.Format("{City:substr(0,3)}", people.First());
// result: "New"
The behavior of SubStringFormatter
in case start index and/or length is out of range can be controlled by SubStringFormatter.OutOfRangeBehavior
:
// SubStringFormatter.SubStringOutOfRangeBehavior.ReturnEmptyString (default behavior):
Smart.Format("{Name:substr(0,999)}", _people.First()) == string.Empty;
// SubStringFormatter.SubStringOutOfRangeBehavior.ReturnStartIndexToEndOfString:
Smart.Format("{Name:substr(0,999)}", _people.First()) == "Long John"
// SubStringFormatter.SubStringOutOfRangeBehavior.ThrowException:
Smart.Format("{Name:substr(0,999)}", _people.First()) // throws FormattingException
Note:
The SubStringFormatter is included in the default SmartFormatter instance.
- 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