By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

String.LastIndexOf() Am I Retarded?

P: n/a
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?

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
Apr 13 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"senfo" <en**********@yahoo.comI-WANT-NO-SPAMwrote in message
news:uc**************@TK2MSFTNGP06.phx.gbl...
>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
>
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

Apr 13 '07 #2

P: n/a
Bill Butler wrote:
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

Well now...I must say that starting from the right is probably the last
thing I would have thought about trying! I was just starting to go
through it in the Reflector when you wrote back.

Thank you very much for your help! I seriously thought I was loosing my
mind!

--
Sean

website: http://senfo.blogspot.com
Apr 13 '07 #3

P: n/a
On Fri, 13 Apr 2007 13:20:49 -0700, Bill Butler <qw****@asdf.comwrote:
[...]
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 neglects to point out that the from the startIndex it will work
towards the front of the string.
I disagree that the documentation neglects to mention this fact. From the
MSDN documentation
(http://msdn2.microsoft.com/en-us/lib...(vs.80).aspx):

This method begins searching at the startIndex character
position of this instance and proceeds backwards towards
the beginning until either value is found or count character
positions have been examined

Just goes to show...it always pays to actually read the documentation. :)

Pete
Apr 13 '07 #4

P: n/a
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 13 Apr 2007 13:20:49 -0700, Bill Butler <qw****@asdf.com>
wrote:
<snip>
>It neglects to point out that the from the startIndex it will work
towards the front of the string.

I disagree that the documentation neglects to mention this fact. From
the MSDN documentation
(http://msdn2.microsoft.com/en-us/lib...(vs.80).aspx):

This method begins searching at the startIndex character
position of this instance and proceeds backwards towards
the beginning until either value is found or count character
positions have been examined

Just goes to show...it always pays to actually read the documentation.
:)
I did read the documentation

First it says:
Reports the index position of the last occurrence of the specified
Unicode character in a substring within this instance. The search starts
at a specified character position and examines a specified number of
character positions.

No mention of backwards searching in there there.

Next it says

value
A Unicode character to seek.
startIndex
The starting position of a substring within this instance.
count
The number of character positions to examine.
Still no mention of it

Unfortunately, I did miss the comment in the remarks section....oops

I still stand by my statement that the documentation is lacking. It is
not a standard usage of startIndex and length. It should be clearly
pointed out up front.
I am glad to see that the remark WAS actually in there, I just wish is
was more prominent

Bill
Apr 13 '07 #5

P: n/a
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.comwrote:
>Just goes to show...it always pays to actually read the documentation.
:)

I did read the documentation
Not all of it.
First it says:
Reports the index position of the last occurrence of the specified
Unicode character in a substring within this instance. The search starts
at a specified character position and examines a specified number of
character positions.

No mention of backwards searching in there there.
No mention of forward searching either.
Next it says

value
A Unicode character to seek.
startIndex
The starting position of a substring within this instance.
count
The number of character positions to examine.
Still no mention of it
And yet, those sections are also accurate, even if incomplete.
Unfortunately, I did miss the comment in the remarks section....oops

I still stand by my statement that the documentation is lacking. It is
not a standard usage of startIndex and length. It should be clearly
pointed out up front.
I'm not going to try to claim that the documentation is flawless. I agree
that it would be nice if the summary were more clear. But the fact is, in
the MSDN documentation the "Remarks" section is where one always has to go
in order to learn about actual usage of the documented element. This is
no different, and it's definitely not true that the documentation
"neglects to point out that the [sic] from the startIndex it will work
towards the front of the string".

That's my point.
I am glad to see that the remark WAS actually in there, I just wish is
was more prominent
And I don't see anything wrong with that wish. But that's not what you
wrote the first time. :)

Pete
Apr 13 '07 #6

P: n/a
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.com>
wrote:
<snip>
>I am glad to see that the remark WAS actually in there, I just wish
is
was more prominent

And I don't see anything wrong with that wish. But that's not what
you wrote the first time. :)
That is true, but I have already conceded this point.
>Unfortunately, I did miss the comment in the remarks section....oops
Bill
Apr 14 '07 #7

P: n/a
Peter Duniho wrote:
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.comwrote:
>>Just goes to show...it always pays to actually read the documentation.
:)

I did read the documentation

Not all of it.
I sincerely hope you don't design user interfaces for anybody. If it's
unclear, people will miss it.

A lot of us are in a hurry and don't exactly have the time, nor the
patience to sit and read every single MSDN page, in its entirety. Two
of us missed that section and we point out a flaw in the documentation,
yet you justify it by remarking that it was in there; but on an entirely
different page from where we looked. It was unclear and it is certainly
a design flaw that has a very simple fix.
>First it says:
Reports the index position of the last occurrence of the specified
Unicode character in a substring within this instance. The search starts
at a specified character position and examines a specified number of
character positions.

No mention of backwards searching in there there.

No mention of forward searching either.
Most people in America read from left to right. In addition to this,
most of the string functions in the .NET framework (that I can think of)
are designed to work from left to right. When you think about it, it
makes sense for LastIndexOf() to work backwards. It should, however, be
clearly pointed out on every page that describes the functionality of
the method.

--
Sean

website: http://senfo.blogspot.com
Apr 16 '07 #8

P: n/a
On Mon, 16 Apr 2007 05:44:07 -0700, senfo
<en**********@yahoo.comI-WANT-NO-SPAMwrote:
A lot of us are in a hurry and don't exactly have the time, nor the
patience to sit and read every single MSDN page, in its entirety.
IMHO, if you don't have the time to at least read in its entirety the ONE
page that applies to the API you're having trouble with, you should not be
programming.
Two of us missed that section and we point out a flaw in the
documentation
yet you justify it by remarking that it was in there
What part of "I'm not going to try to claim that the documentation is
flawless" did you have trouble understanding? How in the world am I
"justifying" the flaw in the documentation?
but on an entirely different page from where we looked.
What "different page"? It's on THE page for THE String.LastIndexOf()
documentation.
It was unclear and it is certainly a design flaw that has a very simple
fix.
Documentation flaw? Sure, I agree. Design flaw? That's a highly
subjective claim, and I don't agree with it. I doubt the .NET Framework
designers do either.
[...] It should, however, be clearly pointed out on every page that
describes the functionality of the method.
It is on every web page that describes the functionality of the method.
Should the search behavior be mentioned in more places on each of those
pages? Probably. So? I didn't disagree with that.

Pete
Apr 16 '07 #9

P: n/a
senfo <en**********@yahoo.comI-WANT-NO-SPAMwrote:
I did read the documentation
Not all of it.

I sincerely hope you don't design user interfaces for anybody. If it's
unclear, people will miss it.

A lot of us are in a hurry and don't exactly have the time, nor the
patience to sit and read every single MSDN page, in its entirety.
I don't read every page in its entirety. However, if I think something
isn't behaving as I expect it to, I *would* read the appropriate page
in its entirety before going any further.
Two of us missed that section and we point out a flaw in the
documentation, yet you justify it by remarking that it was in there;
but on an entirely different page from where we looked. It was
unclear and it is certainly a design flaw that has a very simple fix.
You posted about String.LastIndexOf(char, int, int). That's the method
whose documentation you should have been looking at, and it certainly
*is* in there.
No mention of backwards searching in there there.
No mention of forward searching either.

Most people in America read from left to right.
Which is why the "default" is to go forwards, with IndexOf. I think
it's pretty intuitive that "LastIndexOf" would work backwards. And of
course, that's how it's documented.
In addition to this,
most of the string functions in the .NET framework (that I can think of)
are designed to work from left to right. When you think about it, it
makes sense for LastIndexOf() to work backwards. It should, however, be
clearly pointed out on every page that describes the functionality of
the method.
Could you point out which page it's *not* specified on? I looked
through all 9 overloads mentioned in MSDN, and it's on all of them.
It's not on the page which just lists the overloads, but as that only
has one sentence on it, I don't think it's unreasonable to think that
there might be more information in the page for the specific overload
you're using.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 16 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.