String LastIndexOf bug ?

  • Thread starter Thread starter Ivar
  • Start date Start date
I

Ivar

Hi,

string s = "aXXa";
Console.WriteLine(s.LastIndexOf("XX",0));
Console.WriteLine(s.LastIndexOf("XX"));
Console.WriteLine(s.IndexOf("XX",0));
Console.WriteLine(s.IndexOf("XX"));

Result:
-1 (LastIndexOf must also return 1, but gives -1) Is it a bug ?
1 - ok
1 - ok
1 - ok

In documentation it says:
Reports the index position of the last occurrence of a specified String
within this instance. The search starts at a specified character position.
 
No it's not a bug: The LastIndexOf() search works backwards from the
position specified. Hence there is no match for XX from 0.

Hi,

string s = "aXXa";
Console.WriteLine(s.LastIndexOf("XX",0));
Console.WriteLine(s.LastIndexOf("XX"));
Console.WriteLine(s.IndexOf("XX",0));
Console.WriteLine(s.IndexOf("XX"));

Result:
-1 (LastIndexOf must also return 1, but gives -1) Is it a bug ?
1 - ok
1 - ok
1 - ok

In documentation it says:
Reports the index position of the last occurrence of a specified String
within this instance. The search starts at a specified character position.
 
The search begins at the startIndex character position of this instance and
precedes backwards towards the beginning until either value is found or the
Yes I see now.
 
Back
Top