v2 SubString - axuno/SmartFormat GitHub Wiki
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.