String LastIndexOf bug ?

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

Siva M

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

Ivar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top