★ wanayoo — archive 1999 https://developer.mozilla.org/cs/docs/Web/JavaScript/Reference/Global_Objects/String/substrNouvelle recherche | Portail wanayoo

U.S. visitors: Help Mozilla protect Net Neutrality by signing our petition to Congress (http://mzl.la/nnpetition) on Wednesday, September 10th! See our blog post to learn more: https://blog.mozilla.org/netpolicy/2014/09/04/a-day-of-action-to-protect-net-neutrality/

mozilla
Your Search Results

    String.prototype.substr()

    Souhrn

    Metoda  substr() vrací znaky řetězce od počítečního indexu start o určité délce length.

    Syntaxe

    str.substr(start[, length])

    Parametry

    start
    Index počátku čtení.
    length
    Délka znaků k přečtení.

    Popis

    start je index znaku. Indexem prvního znaku v řetězci je 0 a index posledního je o 1 méně než je délka řetězce. substr začíná číst znaky na indexu start a čte právě length znaků. (když se dostane ke konci řetězce, tak se automaticky ukončí a tak vrátí i méně znaků než je velikost length)

    Když je start kladné a je větší a nebo rovno délce řetězce, substr vrací prázdný řetězec.

    Když je start záporný, bere se to jako index od konce řetězce. Když je start záporný a abs(start) je větší než délka řetězce, substr použije 0 jako počáteční index. Poznámka: popsané zacházení se zápornými hodnotami v argumentu start není podporováno v Microsoft JScript.

    Když je length 0 nebo záporné čílo, substr vrací prázdný řetězec. Když je argument length vynechaný, substr čte až do konce řetězce.

    Příklady

    Příklad: Použití substr

    var str = "abcdefghij";
    
    console.log("(1,2): "    + str.substr(1,2));   // (1,2): bc
    console.log("(-3,2): "   + str.substr(-3,2));  // (-3,2): hi
    console.log("(-3): "     + str.substr(-3));    // (-3): hij
    console.log("(1): "      + str.substr(1));     // (1): bcdefghij
    console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
    console.log("(20, 2): "  + str.substr(20,2));  // (20, 2):
    

    Polyfill

    Microsoft JScript nepodporuje záporné hodnoty počátečního indexu. Když si přejete použít tuto vlastnost, můžete použít následující polyfill.

    // only run when the substr function is broken
    if ('ab'.substr(-1) != 'b') {
      /**
       *  Get the substring of a string
       *  @param  {integer}  start   where to start the substring
       *  @param  {integer}  length  how many characters to return
       *  @return {string}
       */
      String.prototype.substr = function(substr) {
        return function(start, length) {
          // did we get a negative start, calculate how much it is
          // from the beginning of the string
          if (start < 0) start = this.length + start;
          
          // call the original function
          return substr.call(this, start, length);
        }
      }(String.prototype.substr);
    }

    Specifikace

    Specifikace Stav Komentář
    ECMAScript 3rd Edition. Standard Definováno v (informative) Compatibility Annex B.
    Implemented in JavaScript 1.0
    ECMAScript Language Specification 5.1th Edition (ECMA-262) Standard Definováno v (informative) Compatibility Annex B
    ECMAScript Language Specification 6th Edition (ECMA-262) Draft Definováno v (normative) Annex B for Additional ECMAScript Features for Web Browsers

    Kompatibilita v prohlížečích

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Základní podpora (Yes) (Yes) (Yes) (Yes) (Yes)
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Základní podpora (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

    Poznámka: Až do verze 3.6, měl Firefox bug, který způsoboval, že substr vrací prázdný řetězec, když argument length byl nastaven jako undefined.

     

    Související

    Document Tags and Contributors

    Contributors to this page: PageOnline
    Last updated by: PageOnline,