Case-sensitive \'Starts With\'


/ Published in: JavaScript
Save to your folder(s)

A simple Javascript function to tell if one string starts with another.

Usage:

if (myString.startsWith("anything") {
do something…
}


NOTE: As always, make sure this is defined before it is used!


Copy this code and paste it in your HTML
  1. if (typeof String.prototype.startsWith != 'function') {
  2. String.prototype.startsWith = function(str) {
  3. return (this.lastIndexOf(str, 0) === 0);
  4. };
  5. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.