Class FormatBuilder
- Namespace
- Bonsai.Expressions
- Assembly
- Bonsai.Core.dll
Represents an expression builder that applies a string formatting operation on elements of an observable sequence.
The Format
operator is a Transform that can be applied on sequences of any type. Each of the elements in the sequence will be converted to a string value using the specified Format. The format string follows the same syntax of the String.Format method. The Selector property can be used to specify the order of the values that will be converted to strings and inserted at a specified place in the format string.
If no format string is specified, the default ToString method is used to convert each value in the sequence.
Examples
Note
Below you can find various applications of the Format
operator. For more formatting examples and a comprehensive list of supported format strings for different data types, see the extended discussion section of the String.Format method.
Insert a string
You can use the Format
operator to insert the value of a sequence into another string. For example, the following workflow and format string visualizes the value and timestamp of each tick of a timer:
Received value {0} at time {1}.
Both {0}
and {1}
are format items. The index 0
refers to the first member specified in the Selector property of the Format
node (in this case Value
). Index 1
refers to the second member (in this case Timestamp
). The output will be similar to:
Received value 9 at time 12/25/2022 22:15:06 +00:00.
We can specify a format string for the second member to remove the date part of the timestamp:
Received value {0} at time {1:T}.
Which will then produce the following output:
Received value 9 at time 22:15:06.
Format a file name
You can format the names of data files dynamically using the Format
operator. This is useful to generate file names relative to a common base path which can be easily changed in only one place:
public class FormatBuilder : SelectBuilder, IExpressionBuilder
- Inheritance
-
FormatBuilder
- Implements
- Inherited Members
- Extension Methods
Properties
Format
Gets or sets the composite format string used to specify the output representation.
public string Format { get; set; }
Property Value
Selector
Gets or sets a string used to specify the properties that will be included in the output representation.
public string Selector { get; set; }
Property Value
Methods
BuildSelector(Expression)
Returns the expression that applies a string formatting operation on the specified input parameter to the selector result.
protected override Expression BuildSelector(Expression expression)
Parameters
expression
ExpressionThe input parameter to the selector.
Returns
- Expression
The Expression that applies a string formatting operation on the input parameter to the selector result.