startsWith() : Check String Prefix
On this page
Summary
Check if a string starts with a specified prefix.
Signature
startsWith(str, prefix)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
str |
String | Yes | The string to check |
prefix |
String | Yes | The prefix to look for |
Returns
Type: Boolean
true if the string starts with the prefix, false otherwise.
Examples
Protocol Detection
Data:
doc.Params["model"] = new {
url = "https://example.com"
};
Output:
<span class="secure">🔒 Secure</span>
File Type Detection
Data:
doc.Params["model"] = new {
files = new[] {
new { name = "report_2024.pdf" },
new { name = "data.csv" },
new { name = "report_summary.xlsx" }
}
};
Output:
<div class="file-report">
<span class="icon">📄</span> report_2024.pdf
</div>
<div class="file-report">
<span class="icon">📄</span> report_summary.xlsx
</div>
Prefix-Based Filtering
Phone Number Validation
Data:
doc.Params["model"] = new {
phone = "+1-555-123-4567"
};
Output:
<p>US/Canada: +1-555-123-4567</p>
Notes
- Case-sensitive by default
- Returns false if prefix is not found at beginning
- Empty prefix returns true
- For case-insensitive check, use with
toLower():startsWith(toLower(str), toLower(prefix)) - For suffix checking, use
endsWith() - For substring anywhere, use
contains()