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

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


  <span class="secure">🔒 Secure</span>

  <span class="insecure">âš  Not Secure</span>

Data:

doc.Params["model"] = new {
    url = "https://example.com"
};

Output:

<span class="secure">🔒 Secure</span>

File Type Detection


  
    <div class="file-report">
      <span class="icon">📄</span> 
    </div>
  

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


  
    <span class="code-professional"></span>
  
    <span class="code-standard"></span>
  

Phone Number Validation


  <p>US/Canada: </p>

  <p>UK: </p>

  <p>International: </p>

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()

See Also