String.slice() is missing

Posted on
  • Hi All,

    It seems that String.slice() is missing.

    http://www.w3schools.com/jsref/jsref_sli­ce_string.asp

    I was wondering if this will be implemented in espruino?

    Regards,

    Alex

  • I'll look into it - it looks amazingly similar to String.substring though - it seems a shame to have 3 different ways of getting substrings!

    For now, something like:

    String.prototype.slice = function(a,b) {
      if (b<0) b+=this.length;
      return this.substring(a,b);
    }
    

    will probably do all you need.

  • Hi Gordon,

    Thats true, though slice can also just have one argument. For last element in string you can do string.slice(-1). Which ofcourse you can also do with string.substring(string.lenght-1, string.length). It would be nice to have :)

    Regards,

    Alex

  • You can also use "Hello".substr(-1) too.

    I think I can put it in and basically re-use the same code - it's just frustrating that JS has 3 functions which all do basically the same thing in subtly different ways :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

String.slice() is missing

Posted by Avatar for Alex @Alex

Actions