string.LastIndexOf() Bug?
Asked Answered
S

9

44

does anyone know why:

"  <exception>".LastIndexOf("<",0) returns -1 (wrong)

while

"  <exception>".LastIndexOf("<") returns 2 (right)

and

"<exception>".LastIndexOf("<",0) returns 0 (right)

Is this a bug or am I misunderstanding the LastIndexOf-Method?

Sweepstakes answered 14/9, 2012 at 8:57 Comment(0)
P
79

You are misunderstanding that particular overload of the LastIndexOf method.

The docs state the following:

The search starts at a specified character position and proceeds backward toward the beginning of the string.

Note that it says backward. So, if you start at position 0, there is no "<" substring at that position or in front of that position and hence the result is -1.

In contrast, if you use the overload that takes only the substring, the search will start at the end of the string and hence correctly find the indicated substring.

Pegeen answered 14/9, 2012 at 9:2 Comment(6)
Ah ok thank you very much. I just read the description in the intellisense-menu, so I didnt know that it proceeds backward. ;)Sweepstakes
a more interesting question, imo, is why those choose to implement it this way.Toxoplasmosis
@Toxoplasmosis this way you can stop after you find the first instance.Christenson
@antony.trupe: Because like this, you can step by step find all occurrences starting at the end of the string.Pegeen
@antony.trupe: If you think about it, it's the only sensible way to do it. If the purpose of the function is to get the index of the last occurrence of the string, then providing a starting index to work forward from would be meaningless.Gilgilba
Ok now with the upvotes, this must easily be the most overrated answer I have written so far. If you want to upvote an answer, please upvote answers whose authors have put thought into their solution and explained these thoughts, rather than just copied the docs.Pegeen
T
22

The string, int32 overload of LastIndexOf says, in the description, "Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string."

Thus if you pass in 0, it will only check the first character, not check the whole string from 0.

Teague answered 14/9, 2012 at 9:1 Comment(0)
F
11

The second parameter does not do what you seem to think it does:

LastIndex(char value, int startIndex)

the startIndex is the char to start searching backwards through the string, so if you pass a 0 then only the first char is checked...

To check the whole string from the end you would have to pass the length of the string -1.

see MSDN String.LastIndex

Flagship answered 14/9, 2012 at 9:5 Comment(0)
B
9

The docs (http://msdn.microsoft.com/en-us/library/bc3z4t9d.aspx#Y0) say:

The search begins at the startIndex character position of this instance and proceeds backward toward the beginning until either value is found or the first character position has been examined. For example, if startIndex is Length - 1, the method searches every character from the last character in the string to the beginning.

(My emphasis)

So this:

"  <exception>".LastIndexOf("<",0)

is beginning at 0 and working backwards, and therefore correctly finding no result and returning -1.

I think the confusion is that LastIndexOf counts backwards, where IndexOf counts forwards.

Billings answered 14/9, 2012 at 9:7 Comment(0)
A
4

I think you're missunderstanding the method.

The second parameter is the character where you start to search. If you search backward from the character in the 0 position... the results are right.

Applewhite answered 14/9, 2012 at 9:2 Comment(0)
N
4

It is a mistake, because LastIndexOf ** () ** search backward from the specified location.

Try this code:

"  <exception>".LastIndexOf("<", 5)

Hope it is useful

Nickienicklaus answered 14/9, 2012 at 9:15 Comment(0)
R
3

That's because the second parameter says that it should start at position zero and look from there and towards the beginning of the string.

You would only find the string if it is at the beginning of the string, and only if you are looking for a string that is one character long:

"<exception>".LastIndexOf("<",0)  // returns 0

"<exception>".LastIndexOf("<ex",0) // returns -1
Rheotaxis answered 14/9, 2012 at 9:2 Comment(0)
B
2

It's OK the first expression finds the < symbol from the 0 element of string to begin e. g. no such simbol find; the second one search through whole the string and find in position 2 and last one searching from 0 simbol which equal < and return the 0 position.

msdn) link

Bridle answered 14/9, 2012 at 9:6 Comment(0)
G
2
  • haystack.LastIndexOf("needle") searches a string for the last occurence of a "needle" -string.

  • The method LastIndexOf and IndexOf returns -1 if the value to search for never occurs.

  • LastIndexOf seeks from <--right-to-left, whereas

  • IndexOf seeks from left-to-right-->

    • The character indices of the string start at 0 and increment from left to right

    • Both methods assume an offset-index of 0 when no second parameter is supplied

  • The function also exists in JavaScript as lastIndexOf

    • (Note: the mixedcase or camelBack-casing) and that setting an index outside the bounds of the haystring's length is OK in JavaScript, but in C# it will result in a System.ArgumentOutOfRangeException: < 0 || > this.Length Exception.

Examples:

"  <exception>".LastIndexOf("<", 3)
 //> returns 2
"  <exception>".LastIndexOf("<", "  <exception>".Length)
 //> returns 2
"  <exception>".LastIndexOf("<", "  <exception>".Length+1)
 //> ArgumentOutOfRangeException
"  <exception>".LastIndexOf("<", 2)
 //> returns 2
"  <exception>".LastIndexOf("<", -1)
 //> returns -1
"  <exception>".LastIndexOf("<", -2)
 //> ArgumentOutOfRangeException
"  <exception>".LastIndexOf("<", 1)
 //> returns -1

Since in the last case no string or character < exists within the haystring's character interval of 0-1, -1 is correctly returned.

Goldwin answered 23/9, 2012 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.