String.LastIndexOf() Am I Retarded?

  • Thread starter Thread starter senfo
  • Start date Start date
S

senfo

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,
 
senfo said:
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
 
Bill said:
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!
 
[...]
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/library/ms131439(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
 
Peter Duniho said:
I disagree that the documentation neglects to mention this fact. From
the MSDN documentation
(http://msdn2.microsoft.com/en-us/library/ms131439(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
 
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
 
Peter Duniho said:
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.
Bill
 
Peter said:
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.
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.
 
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
 
senfo said:
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.
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.
 
LastIndexOf method works backwards!!!

try this:

string myString = "This is a test";
int index = myString.LastIndexOf(' ', mySting.Length());

have a nice day



senfo wrote:

String.LastIndexOf() Am I Retarded?
13-Apr-07

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
int index = myString.LastIndexOf(' ', 1); // exceptio
int index = myString.LastIndexOf(' ', 4); // returns

Have I completely lost my mind

Thank you in advance

--
Sea

website: http://senfo.blogspot.com

Previous Posts In This Thread:

String.LastIndexOf() Am I Retarded?
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
int index = myString.LastIndexOf(' ', 1); // exceptio
int index = myString.LastIndexOf(' ', 4); // returns

Have I completely lost my mind

Thank you in advance

--
Sea

website: http://senfo.blogspot.com

Re: String.LastIndexOf() Am I Retarded?

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)

Parameter
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
S
In your case it will start at index zero and attempt to march 6 spots to
the left (-6), thus the ArgumentOutOfRangeException

It looks lik
int index = myString.LastIndexOf(' ', 6, 6)
is what you really wante

Hope this help
Bill

Re: String.LastIndexOf() Am I Retarded?
Bill Butler wrote

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

--
Sea

website: http://senfo.blogspot.com

Re: String.LastIndexOf() Am I Retarded?

I disagree that the documentation neglects to mention this fact. From the
MSDN documentation
(http://msdn2.microsoft.com/en-us/library/ms131439(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

Re: String.LastIndexOf() Am I Retarded?
<snip>

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

Re: String.LastIndexOf() Am I Retarded?


Not all of it.


No mention of forward searching either.


And yet, those sections are also accurate, even if incomplete.


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.


And I don't see anything wrong with that wish. But that's not what you
wrote the first time. :)

Pete

Re: String.LastIndexOf() Am I Retarded?
<snip>

That is true, but I have already conceded this point.

Bill

Re: String.LastIndexOf() Am I Retarded?
Peter Duniho wrote:

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.


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

Re: String.LastIndexOf() Am I Retarded?
On Mon, 16 Apr 2007 05:44:07 -0700, senfo


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.


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?


What "different page"? It's on THE page for THE String.LastIndexOf()
documentation.


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

Re: String.LastIndexOf() Am I Retarded?

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.


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.


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.


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 - <[email protected]>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Submitted via EggHeadCafe - Software Developer Portal of Choice
More Fun with Fluent NHibernate Automapping
http://www.eggheadcafe.com/tutorial...9-81ee42171b00/more-fun-with-fluent-nhib.aspx
 
LastIndexOf method works backwards!!!

Of course it works backwards. That's why we have LASTindexof. If you
want to work forwards, use IndexOf.

Read the "Remarks" in the documentation: "This method begins searching
at the startIndex character position of this instance and proceeds backward
toward the beginning [...]".
 
Back
Top