length() : Get String/Array Length
On this page
Summary
Get the length of a string or count of items in an array.
Signature
length(value)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
value |
String/Array | Yes | The string or array to measure |
Returns
Type: Integer
The number of characters in the string or items in the array.
Examples
String Length
Data:
doc.Params["model"] = new {
text = "Hello World"
};
Output:
<p>Length: 11 characters</p>
Array Count
Data:
doc.Params["model"] = new {
items = new[] { "Item 1", "Item 2", "Item 3" }
};
Output:
<p>Total items: 3</p>
Conditional Truncation
Character Limit Validation
Data:
doc.Params["model"] = new {
comment = "This is a sample comment."
};
Output:
<p>This is a sample comment.</p>
List Size Display
Data:
doc.Params["model"] = new {
products = new[] {
new { name = "Widget A" },
new { name = "Widget B" },
new { name = "Widget C" }
}
};
Output:
<h3>Products (3)</h3>
<ul>
<li>Widget A</li>
<li>Widget B</li>
<li>Widget C</li>
</ul>
Notes
- Works with both strings and arrays
- Returns character count for strings
- Returns item count for arrays/collections
- Returns 0 for empty string or array
- Null values may throw exception
- For collection operations, also see
count()function