A few basic conveniences that many languages (e.g., Java, C#, Python, Ruby) have by default are missing in the ECMAScript string library.
IMPLEMENTATION NOTE: These code snippets are given as rough descriptions of intended behavior. They are NOT tested or suggested as production-quality polyfills.
NOTE: all the “behaves the same as” is defined in terms of the “original value of” the various methods used.
String.prototype.startsWithBehaves the same as:
String.prototype.startsWith = function(s) { return this.indexOf(s) === 0; };
String.prototype.endsWithBehaves the same as:
String.prototype.endsWith = function(s) { var t = String(s); var index = this.lastIndexOf(t); return index >= 0 && index === this.length - t.length; };
String.prototype.containsBehaves the same as:
String.prototype.contains = function(s) { return this.indexOf(s) !== -1; };
String.prototype.toArrayBehaves the same as:
String.prototype.toArray = function() { return this.split(''); };
— Dave Herman 2011/03/13 18:54
Questions:
— Luke Hoban 2011/11/15