PhilOfPerth Posted September 27, 2014 Share Posted September 27, 2014 I have noticed that when the slice() method is used with negative index parameters, the first index counts the end of the string as -1, while the second counts it as 0. This is not noted in the W3C tutorial. Is this intended, or is it a bug? For example, if word="abcdefg" then slice.word(-5,-3) gives "cd" and not "cde" as I would expect. Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2014 Share Posted September 27, 2014 If the start of the search for is five positions back from the end and you end the search three positions back then there are only two letters to show: Look which letters are between -5 and -3 in this graph: 0 1 2 3 4 5 6 7 | a | b | c | d | e | f | g | -7 -6 -5 -4 -3 -2 -1 The end of the string doesn't have a negative number associated to it. -1 means one position away from the end of the string. Link to comment Share on other sites More sharing options...
PhilOfPerth Posted September 27, 2014 Author Share Posted September 27, 2014 So the START param includes the named position, but the END position doesn't? It sounds a bit ambiguous to me. Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2014 Share Posted September 27, 2014 (edited) Both the start and the end parameters place -1 at the same place. Look at the previous diagram again: if start is -1 then the letter than follows it ("g") will be shown. If the end is -1 then letters between the start and end are shown which don't include "g" Here's another diagram: Start is -1 a b c d e f [ g ] +---+---+---+---+---+---+---+ End is -1: a b c d e [ f ] g +---+---+---+---+---+---+---+ Edited September 27, 2014 by Ingolme Link to comment Share on other sites More sharing options...
PhilOfPerth Posted September 27, 2014 Author Share Posted September 27, 2014 Ok, thanks. I think I get it. The start param can be considered INCLUSIVE of itself and later chars, while the end param is EXCLUSIVE of itself and further chars. Thanks. Sorry to be pedantic. It's my CDO again! Link to comment Share on other sites More sharing options...
Ingolme Posted September 27, 2014 Share Posted September 27, 2014 You could view it that way, but I find it more helpful to see it as the position of a cursor between the letters, the 0 position being right before the first letter. Negative values are offsets from the position right after the last letter. "a" is not letter zero, it's the letter between positions zero and one. therefore: .slice(0, 1) Link to comment Share on other sites More sharing options...
PhilOfPerth Posted September 27, 2014 Author Share Posted September 27, 2014 Gotit ! Thanks both. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now