Skip to main content Link Search Menu Expand Document (external link)

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

<p>Length:  characters</p>

Data:

doc.Params["model"] = new {
    text = "Hello World"
};

Output:

<p>Length: 11 characters</p>

Array Count

<p>Total items: </p>

Data:

doc.Params["model"] = new {
    items = new[] { "Item 1", "Item 2", "Item 3" }
};

Output:

<p>Total items: 3</p>

Conditional Truncation


  
    <p>...</p>
  
    <p></p>
  

Character Limit Validation


  <p class="error">Comment too long (/200 characters)</p>

  <p></p>

Data:

doc.Params["model"] = new {
    comment = "This is a sample comment."
};

Output:

<p>This is a sample comment.</p>

List Size Display

<h3>Products ()</h3>
<ul>

  <li></li>

</ul>

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

See Also