contains() : Check if String Contains Substring
On this page
Summary
Check if a string contains a specified substring.
Signature
contains(str, search)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
str |
String | Yes | The string to search in |
search |
String | Yes | The substring to search for |
Returns
Type: Boolean
true if the substring is found, false otherwise.
Examples
Simple Search
Data:
doc.Params["model"] = new {
description = "This is a premium quality product"
};
Output:
<span class="badge-premium">Premium Product</span>
Filter Items
Data:
doc.Params["model"] = new {
products = new[] {
new { name = "Widget" },
new { name = "Widget Pro" },
new { name = "Gadget Pro Max" }
}
};
Output:
<div class="product-pro">
<h3>Widget Pro</h3>
<span>Professional Edition</span>
</div>
<div class="product-pro">
<h3>Gadget Pro Max</h3>
<span>Professional Edition</span>
</div>
Keyword Highlighting
Email Validation
Data:
doc.Params["model"] = new {
email = "user@example.com"
};
Output:
<p>Email: user@example.com</p>
Notes
- Case-sensitive by default
- Returns false if search string is not found
- Empty search string returns true
- For case-insensitive search, use with
toLower():contains(toLower(str), toLower(search)) - For position information, use
indexOf() - Returns false for null values