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?
Java String lastIndexOf() Method The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string. Tip: Use the indexOf method to return the position of the first occurrence of specified character(s) in a string.
The lastIndexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the last occurrence of the specified substring.
strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .
indexOf() method returns the position of the first occurrence of a specified value in a string. string. lastIndexOf() method returns the position of the last occurrence of a specified value in a string.
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.
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
.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With