Felix Riesterer: Frage zum Wiki-Artikel „substr“

Beitrag lesen

problematische Seite

Hallo Ingrid,

das Problem daran ist für mich, dass es für "abcdefg".substr(-2) // "fg" keine sinnvolle Entsprechung mit String.substring() gibt.

die MDN pflichtet bei und sagt:

Although you are encouraged to avoid using substr(), there is no trivial way to migrate substr() to either slice() or substring() in legacy code without essentially writing a polyfill for substr(). For example, str.substr(a, l), str.slice(a, a + l), and str.substring(a, a + l) all have different results when str = "01234", a = 1, l = -2 — substr() returns an empty string, slice() returns "123", while substring() returns "0". The actual refactoring path depends on the knowledge of the range of a and l.

Liebe Grüße

Felix Riesterer