Skip to content
axunonb edited this page Feb 6, 2022 · 1 revision

The SubStringFormatter lets you output parts of an input string.

Examples

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.

Clone this wiki locally