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