inicio mail me! sindicaci;ón

startswith() and endswith() in JavaScript 1.5

MochiKit is a great JS library but JavaScript itself kinda sucks at string manipulation (if you’re a Pythonista it sucks a lot).

While I was fixing and improving the features of a web application I ended writing awful code to check if some string was at the beginning of some other string. I, then, decided to write a JavaScript version of Python’s startswith and endswith.

The two functions are in with.js and their tests are in test_with.js.

I also sent a patch to MochiKit so if they accept it we Pythonistas can all have our little sense of being home (and actually MochiKit does a great job to make Python developers feel in the right place!).

Related posts

  • E, episode one (First glance)
  • Django community please stand up (for DB2)
  • Updates from Python SVN
  • Longhorn preview is coming out..
  • Avalon Imaging Samples series
  • Gravatar

    runeh said,

    August 17, 2006 @ 11:06 am

    Hi, I took a quick stab at rewriting startswith. It seemed a bit more verbose than neccessary. And it should be a prototype function on the String object, so you can do stuff like myString.startswith(needle);

    Anyway, here’s the code. Hopefully not too mangled by the formater. It passes all the tests you defined.

    String.prototype.startswith = function(s, from , to) { from = from || 0; to = to || 9999999; // Number.MAX_VALUE seems to bug with string.slice() s = typeof s == “string” ? [s] : s; for ( var n=0; n

    Gravatar

    Lawrence said,

    August 17, 2006 @ 1:56 pm

    it’s verbose because it’s C-style coded to be somewhat fast. And it explicitly not tied to the String.prototype because he was conceived to enter MochiKit Library. Anyway, I don’t see your code :-(

    Gravatar

    runeh said,

    August 17, 2006 @ 5:08 pm

    I’m pretty sure using “c-style” isn’t doing any good here. String.indexOf is implemented in native code. As is String.slice, so they’re bound to be fast. Odds are, I didn’t have time to benchmark it, that the verbose implementation is slower. Mostly because it uses helper functions. Setting up a calling context etc. for each call is realtively speaking an expensive operation.

    I’ve not looked at mochikit. They don’t like prototypes? I guess there might be cross browser reasons for that. Anyway, converting to a non-prototype implementation is trivial.

    The code didn’t show up properly here either, and I couldn’t find a preview button, so I put a copy here : http://pastebin.ca/136282 . Cheers.

    Gravatar

    Lawrence said,

    August 17, 2006 @ 5:12 pm

    Thanks.

    Gravatar

    Lawrence said,

    August 18, 2006 @ 10:58 am

    Yours doesn’t work in every situation tough :-(.

    Gravatar

    Jay said,

    April 17, 2007 @ 1:18 pm

    Regula expression is faster and eligant… try this: String.prototype.startsWith = function(str) {return (this.match(”^”+str)==str)}

    Gravatar

    This Doesn't work said,

    May 23, 2007 @ 1:37 am

    And after all that loftiness, Runeh’s code doesn’t work. What a douchebag.

    var mystr = “abcd” mystr.startswith(’abd’) -> returns true

    Jay is the pimp of “regula expressions” :-)

    Gravatar

    javascriptHuh said,

    July 5, 2007 @ 6:55 am

    myStr.indexOf(str)==0 can simulate startWith() in Javascript

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment