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.