Sub String formatter
Output part of a string inside a Smart String.
Read time 2 minutesLast updated 3 days ago
Output part of a string inside a Smart String.
The Sub String formatter returns a portion of a string value, selected by a start index and an optional length. If the value isn't a string, it's first converted to one with ToString. Apply it explicitly with the name .
substrAnatomy of the substr formatter syntax.
The first argument of the formatter is the start index. The optional second argument is the length. A negative start index counts back from the end of the string, and a negative length counts back from the end.
substrThe start index and length use the split character to separate them. Valid split characters are (comma, the default), (pipe), and (tilde). Configure it with .
,|~SubStringFormatter.SplitCharWhen a value is reached, the formatter writes the Null Display String, which defaults to . Configure it with . If a nested format is supplied instead, the nested format receives the and is responsible for handling it.
null(null)SubStringFormatter.NullDisplayStringnullA supplied format must contain a nested placeholder, otherwise the formatter throws a .
FormattingExceptionSubStringFormatter.OutOfRangeBehavior- ReturnEmptyString returns an empty string. This is the default.
- ReturnStartIndexToEndOfString returns the rest of the string from the start index.
- ThrowException throws a .
FormattingException
Smart String | Argument | Result |
|---|---|---|
| | John |
| | New |
| | John |
| | Jo |
| | Joh |
| | Hello ara Croft |
You can pass the result to a nested placeholder to keep formatting it:
Smart.Format("{0:substr(0,2):{ToLower}}", "ABC"); // "ab"