Skip to content

Instantly share code, notes, and snippets.

@ebouchut
Created July 11, 2014 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebouchut/2ae093a75975ccf0fca9 to your computer and use it in GitHub Desktop.
Save ebouchut/2ae093a75975ccf0fca9 to your computer and use it in GitHub Desktop.
Test if a string start with another string in Javascript
prefix = "http://";
url = "http://example.com";
url.substring(0, prefix.length) === prefix; // true
@ebouchut
Copy link
Author

var startWith = function (string, prefix) {
          if (string) {
            if (prefix === "" ) { return true; }
            if (prefix) {
                return string.substring(0, prefix.length) === prefix;
            }
          }
          return false;
        };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment