Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

split() : Split String into Array

Split a string into an array of substrings based on a separator.


On this page

Signature

split(str, separator)

Parameters

Parameter Type Required Description
str String Yes The string to split
separator String Yes The separator to split on

Returns

Type: Array of strings

An array containing the split substrings.


Examples

Split CSV

<ul>
{{#each split(model.tags, ',')}}
  <li>{{trim(this)}}</li>
{{/each}}
</ul>

Data:

doc.Params["model"] = new {
    tags = "javascript, html, css, react"
};

Output:

<ul>
  <li>javascript</li>
  <li>html</li>
  <li>css</li>
  <li>react</li>
</ul>

Split Full Name

<p>First Name: {{split(model.fullName, ' ')[0]}}</p>
<p>Last Name: {{split(model.fullName, ' ')[1]}}</p>

Data:

doc.Params["model"] = new {
    fullName = "John Doe"
};

Output:

<p>First Name: John</p>
<p>Last Name: Doe</p>

Split Path

<ul class="breadcrumb">
{{#each split(model.path, '/')}}
  <li>{{this}}</li>
{{/each}}
</ul>

Data:

doc.Params["model"] = new {
    path = "home/documents/reports/2024"
};

Output:

<ul class="breadcrumb">
  <li>home</li>
  <li>documents</li>
  <li>reports</li>
  <li>2024</li>
</ul>

Count Occurrences

<p>Words: {{length(split(model.sentence, ' '))}}</p>

Data:

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

Output:

<p>Words: 5</p>

Notes

  • Returns array of substrings
  • Empty strings included if separator appears consecutively
  • If separator not found, returns array with single element (original string)
  • Case-sensitive matching
  • Use with {{#each}} to iterate results
  • Use with trim() to clean up whitespace
  • Opposite of join() function

See Also