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.LastIn dexOf(' ', 0, 6);
The second line throws an ArgumentOutOfRa ngeException 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.LastIn dexOf(' '); // returns 9
int index = myString.LastIn dexOf(' ', 1); // exception
int index = myString.LastIn dexOf(' ', 4); // returns 4
Have I completely lost my mind?
Thank you in advance,
--
Sean
website: http://senfo.blogspot.com 9 11981
"senfo" <en**********@y ahoo.comI-WANT-NO-SPAMwrote in message
news:uc******** ******@TK2MSFTN GP06.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 ArgumentOutOfRa ngeException.
It looks like
int index = myString.LastIn dexOf(' ', 6, 6);
is what you really wanted
Hope this helps
Bill
>
string myString = "This is a test";
int index = myString.LastIn dexOf(' ', 0, 6);
The second line throws an ArgumentOutOfRa ngeException 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.LastIn dexOf(' '); // returns 9
int index = myString.LastIn dexOf(' ', 1); // exception
int index = myString.LastIn dexOf(' ', 4); // returns 4
Have I completely lost my mind?
Thank you in advance,
--
Sean
website: http://senfo.blogspot.com
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 ArgumentOutOfRa ngeException.
It looks like
int index = myString.LastIn dexOf(' ', 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
On Fri, 13 Apr 2007 13:20:49 -0700, Bill Butler <qw****@asdf.co mwrote:
[...]
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
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 13 Apr 2007 13:20:49 -0700, Bill Butler <qw****@asdf.co m>
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
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.co mwrote:
>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
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.co m>
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.
>Unfortunatel y, I did miss the comment in the remarks section....oops
Bill
Peter Duniho wrote:
On Fri, 13 Apr 2007 15:56:03 -0700, Bill Butler <qw****@asdf.co mwrote:
>>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
On Mon, 16 Apr 2007 05:44:07 -0700, senfo
<en**********@y ahoo.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.LastInde xOf()
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 <en**********@y ahoo.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.LastInde xOf(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 "LastIndexO f" 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matt |
last post by:
In test() method:
var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\").
why??
var pos=path.lastIndexOf("\\"); //return -1
But in showFile() method:
We are able to get the filename. The only difference is test()
hardcode the path,
but in showFile() we gets the path from the file dialog.
|
by: Ivar |
last post by:
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 ?
|
by: Mike |
last post by:
Hello
I load a text file into a string. Then I start seraching for a substring that appears many time in the file. Once I find an occurrence of the substirng I have to find the first accurance of " before it (there has to be one). Then I copy the substring starting with the first " until the index of the substring's occurrence
I find the occurrence of the substring without any problems, and in most cases, I find the first " without any...
|
by: Ron |
last post by:
I *just* know there's a function to do this, but I can't
find it:
I want to take the following string:
c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t
tt\xxx\tttt\xxx\explorer.exe-ph33334
into something like:
|
by: koorb |
last post by:
I am trying to read a line from a text file and store it in a DateTime
veritable. Below is a cut down version of my problem code (in my
actual version I am using a try and catch, and I am receiving each
line in a loop to store into the Array)
If I write the string directly into the array it works, but when I use
the line from the file it doesn't go into the DateTime veriable and I
get an error. Is there something I am missing?
Dim...
| |
by: Roshawn |
last post by:
Hi,
I am retrieving a list of book titles from a web service. What I'd like to
do is shorten the titles, if possible. For example, there is a book titled
"Malicious Mobile Code: Virus Protection for Windows". It would be nice if
I could trim the title down to the colon (it would just say Malicious Mobile
Code).
Is there an easy way to accomplish this task?
|
by: Daren |
last post by:
Hi,
I need to be able to split large string variables into an array of
lines, each line can be no longer than 70 chars.
The string variables are text, so I would additionally like the lines
to end at the end of a word, if you catch my drift.
For example, I have a large string variable containing the text:
"I've seen things you people wouldn't believe. Attack ships on fire
|
by: Mathias Weyel |
last post by:
Hi Group!
I encounter a strange behaviour of String.LastIndexOf when specifying
start and count values.
The following works:
string tempString = orgString.SubString(0, mycount);
int index = tempString.LastIndexOf(mychar);
|
by: shapper |
last post by:
Hello,
I need to replace the last comma in a string by " and ". How can I do
this?
Thanks,
Miguel
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |