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?

0

9 Answers 9

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.

6
  • 2
    Ah ok thank you very much. I just read the description in the intellisense-menu, so I didnt know that it proceeds backward. ;)
    – Florian Gl
    Commented Sep 14, 2012 at 9:07
  • 2
    a more interesting question, imo, is why those choose to implement it this way. Commented Sep 14, 2012 at 20:42
  • @antony.trupe this way you can stop after you find the first instance.
    – soandos
    Commented Sep 14, 2012 at 20:47
  • 2
    @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. Commented Sep 14, 2012 at 21:52
  • 4
    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. Commented Sep 16, 2012 at 17:30
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.

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

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.

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.

4

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

Try this code:

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

Hope it is useful

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
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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.