Join the DZone community and get the full member experience.Join For Free
substring (ECMAScript 5.1 Specification Section 22.214.171.124) is the first logical choice to retrieve a part of the string. This
substring function can accept two numbers, the start and the end (exclusive) character position, respectively. In case the end value is smaller than the start, substring is smart enough to swap the values before doing the string extraction. An example of substring is illustrated in this code snippet:
var a = 'The Three Musketeers'; a.substring(4, 9); 'Three' a.substring(9, 4); 'Three'
substr (Section B.2.3). However, the parameters for
substr are the start character position and the numbers of characters to be extracted, respectively. This is shown in the following fragment:
var b = 'The Three Musketeers'; b.substr(4, 9); 'Three Mus' b.substr(9, 4); ' Mus'
Prevention is the best cure. Be advised of your function name!
Published at DZone with permission of Ariya Hidayat , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.