Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.LastIndexOf() Bug?

Tags:

string

c#

indexof

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?

like image 653
Florian Gl Avatar asked Sep 14 '12 08:09

Florian Gl


People also ask

What is lastIndexOf in Java?

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.

What does lastIndexOf mean?

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.

How do you find the last occurrence of a string?

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 .

What are the differences between indexOf () and lastIndexOf ()?

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.


4 Answers

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.

like image 161
O. R. Mapper Avatar answered Sep 22 '22 01:09

O. R. Mapper


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.

like image 34
Rawling Avatar answered Sep 23 '22 01:09

Rawling


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

like image 29
Daniel Avatar answered Sep 24 '22 01:09

Daniel


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.

like image 22
Jude Fisher Avatar answered Sep 20 '22 01:09

Jude Fisher