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

endsWith() : Check String Suffix


On this page

Summary

Check if a string ends with a specified suffix.

Signature

endsWith(str, suffix)

Parameters

Parameter Type Required Description
str String Yes The string to check
suffix String Yes The suffix to look for

Returns

Type: Boolean

true if the string ends with the suffix, false otherwise.


Examples

File Extension Check


  
    <div class="file-pdf">
      <span class="icon">📄</span> 
    </div>
  
    <div class="file-image">
      <span class="icon">🖼️</span> 
    </div>
  

Data:

doc.Params["model"] = new {
    files = new[] {
        new { name = "document.pdf" },
        new { name = "photo.jpg" },
        new { name = "report.docx" }
    }
};

Output:

<div class="file-pdf">
  <span class="icon">📄</span> document.pdf
</div>
<div class="file-image">
  <span class="icon">🖼️</span> photo.jpg
</div>

Domain Validation


  <span class="badge-internal">Internal</span>

  <span class="badge-external">External</span>

Data:

doc.Params["model"] = new {
    email = "john.doe@company.com"
};

Output:

<span class="badge-internal">Internal</span>

URL Path Checking


  <a href="index.html">index.html</a>

  <a href=""></a>

Version Suffix


  <div class="product">
    <h3></h3>
    
      <span class="badge-beta">Beta</span>
    
      <span class="badge-alpha">Alpha</span>
    
      <span class="badge-stable">Stable</span>
    
  </div>

Data:

doc.Params["model"] = new {
    products = new[] {
        new { name = "Product A", version = "1.0.0" },
        new { name = "Product B", version = "2.0.0-beta" },
        new { name = "Product C", version = "3.0.0-alpha" }
    }
};

Output:

<div class="product">
  <h3>Product A</h3>
  <span class="badge-stable">Stable</span>
</div>
<div class="product">
  <h3>Product B</h3>
  <span class="badge-beta">Beta</span>
</div>
<div class="product">
  <h3>Product C</h3>
  <span class="badge-alpha">Alpha</span>
</div>

Notes

  • Case-sensitive by default
  • Returns false if suffix is not found at end
  • Empty suffix returns true
  • For case-insensitive check, use with toLower(): endsWith(toLower(str), toLower(suffix))
  • For prefix checking, use startsWith()
  • For substring anywhere, use contains()

See Also