-
-
Notifications
You must be signed in to change notification settings - Fork 105
SubString _ SubStringFormatter
The SubStringFormatter
lets you output part of an input string.
{ string : substr(start,length) : {format-placeholder} }
Value | formatter name | (arguments) | optional :format |
---|---|---|---|
string | "substr" |
int start int length |
{placeholder} to format the substring |
value: Only strings can be processed. Other objects cause a FormattingException
.
arguments: The start position and the lenght of the sub-string.
format-placeholder: A nested Placeholder
that lets you format the result of the sub-string operation.
string Name
: default issubstr
The name to use a named formatter
char SplitChar
: default is','
string NullDisplayString
:
The string to display fornull
values, defaults tostring.Empty
. It will not be applied, if a format option is provided to the formatter. In this case, the child formatter must handle thenull
result.
SubStringOutOfRangeBehavior OutOfRangeBehavior
:
The behavior when start index and/or length are too big. Defaults toSubStringOutOfRangeBehavior.ReturnEmptyString
Examples use the following variable:
var person = new {Name = "Long John", City = "New York"};
Smart.Format("{Name:substr(5)}", person);
// result: "John"
Smart.Format("{City:substr(0,3)}", person);
// result: "New"
The behavior of SubStringFormatter
in case start index and/or length is out of range can be controlled by SubStringFormatter.OutOfRangeBehavior
:
// SubStringOutOfRangeBehavior.ReturnEmptyString (default behavior):
Smart.Format("{Name:substr(0,999)}", person);
// Outputs: "";
// SubStringOutOfRangeBehavior.ReturnStartIndexToEndOfString:
Smart.Format("{Name:substr(0,999)}", person);
// Outputs: "Long John";
// SubStringOutOfRangeBehavior.ThrowException:
Smart.Format("{Name:substr(0,999)}", person)
// throws FormattingException
The Format argument must contain nested Placeholder
, and may contain literal text.
Smart.Format("{0:substr(0,2):{ToLower}}", "ABC");
// | |
// + format +
// arg
// Outputs: "ab"
Format the substring with literal text and placeholder.
smart.Format("{0:substr(0,2):First 2 chars\\: {ToLower.ToCharArray:list:'{}'| and }}", "ABC");
// Outputs: "First 2 chars: 'a' and 'b'"
- 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