"senfo" <enceladus311@yahoo.comI-WANT-NO-SPAMwrote in message
news:ucyNB5ffHHA.2640@TK2MSFTNGP06.phx.gbl...
Quote:
>I realize it's Friday and I'm probably already on vacation for the
>remainder of the day; but, I have a really, really stupid question. Is
>there a bug in the .NET 2.0 Framework in regards to the LastIndexOf()
>method or am I just not understanding something that should be obvious?
Apparently, I have only ever used the simple overload for this method.
After playing around a bit I can conlude that Your logic is sound based
on the documentation...It is the documentation that is lacking on this
one.
It says:
---------------------------------------------------------------
public int LastIndexOf( char value, int startIndex, int count);
Parameters
value A Unicode character to seek.
startIndex The starting position of a substring within this instance.
count The number of character positions to examine.
-----------------------------------------------------------------
It neglects to point out that the from the startIndex it will work
towards the front of the string.
So
In your case it will start at index zero and attempt to march 6 spots to
the left (-6), thus the ArgumentOutOfRangeException.
It looks like
int index = myString.LastIndexOf(' ', 6, 6);
is what you really wanted
Hope this helps
Bill
Quote:
>
string myString = "This is a test";
int index = myString.LastIndexOf(' ', 0, 6);
>
The second line throws an ArgumentOutOfRangeException exception, which
says, Count must be positive and count must refer to a location within
the string/array/collection.
>
I would like to find the last instance of a space between the
characters at index zero and six.
>
int index = myString.LastIndexOf(' '); // returns 9
int index = myString.LastIndexOf(' ', 1); // exception
int index = myString.LastIndexOf(' ', 4); // returns 4
>
Have I completely lost my mind?
>
Thank you in advance,
>
--
Sean
>
website:
http://senfo.blogspot.com