Using Slice(), Substring() | angular substring
Inthebook,EloquentJavascript:AModernIntroductiontoProgramming[1],MarijnHaverbeketakesthereaderthroughagoodnumberofstring-parsingexamples.And,inhisexamples,HaverbekemakesfrequentuseoftheStringmethod,slice().WhenIthinkofslice(),IthinkofextractingportionsofaJavascriptarray;IdontthinkIwasevenawarethattheJavascriptStringprototypehadaslice()method.Toclearupmyownconfusionandmisconceptions,IwantedtotakeaquicklookatthevariouswaysinwhichJavascriptallowsforpartialstringextraction.FromwhatIcansee,therea...
In the book, Eloquent Javascript: A Modern Introduction to Programming[1], Marijn Haverbeke takes the reader through a good number of string-parsing examples. And, in his examples, Haverbeke makes frequent use of the String method, slice(). When I think of slice(), I think of extracting portions of a Javascript array; I dont think I was even aware that the Javascript String prototype had a slice() method. To clear up my own confusion and misconceptions, I wanted to take a quick look at the various ways in which Javascript allows for partial string extraction.
From what I can see, there are three primary methods for substring extraction:
String.slice( begin [, end ] ) String.substring( from [, to ] ) String.substr( start [, length ] )In all cases, the second argument is optional. If it is not provided, the substring will consist of the start index all the way through the end of the string. For both the slice() and substring() methods, the second argument is exclusive; ...