LastIndexOf() strange behavior

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

string url = http://localhost/subPath/Default.aspx;
k = url.LastIndexOf("/", 0, url.Length);

This throws an exception: Count must be positive and count must refer to a
location within the string/array/collection. Parameter name: count

Why? This seems completely legal to me... yet it fails? Anyone see the
problem?

-SK
 
It might be that Length is a one-based value while the index into the string
is zero-based. Try Length-1. Just a thought.
 
sklett said:
string url = http://localhost/subPath/Default.aspx;
k = url.LastIndexOf("/", 0, url.Length);

This throws an exception: Count must be positive and count must refer to a
location within the string/array/collection. Parameter name: count

Why? This seems completely legal to me... yet it fails? Anyone see the
problem?

Yes - you're asking it to start examining at the first character, and
look backwards for url.Length character positions. It clearly can't do
that, as it can only look in the first position and then stop.

It's unfortunate that neither the documentation nor the error message
are accurate - they're basically only accurate for IndexOf rather than
LastIndexOf. In fact, count must be less than or equal to
(startIndex+1).
 
Just a thought, try URL.LastindexOf("/"). By omiting start index and length,
it should default to check the entire string
 
ahh, very misleading.

See I thought it would start at 'h' (url[0]) and look all the way to
'x'(url[url.Length-1]
in a left to right manner, are you saying that LastIndexOf() starts at
Length-1 and goes to the start? (right to left)

weird... Thanks for the response! :)
 
My mistake
That was actually a typo, in the app I have Length-1. Thanks for the
response :)
 
Hi-

given this string:
url = http://www.SomeDomain.com/Products/?id=333

What I wanted to do was find this:
"Products"

So I was trying something like this:
int i, j;
i = url.LastIndexOf("/");
j = url.LastIndexOf("/", 0, i-1); // which should stop it short of the
actual last '/' character

string chunk = url.SubString(i, j-1);


or something like that. Of course I see how ugly this is, but I was tired
and it was late and it bugged me that I could get the LastIndexOf(string,
start, count) to work the way I figured it should.

Now I'm just getting the bits I need out of the Request.Uri object ;)
(maintains collection is url stirng chunks split at the '/')
 
neverstill said:
ahh, very misleading.

See I thought it would start at 'h' (url[0]) and look all the way to
'x'(url[url.Length-1]
in a left to right manner, are you saying that LastIndexOf() starts at
Length-1 and goes to the start? (right to left)

weird... Thanks for the response! :)

Not weird at all - that's why it's LastIndexOf rather than IndexOf. If
you want it to look from left to right, just use IndexOf.
 
wow....
uhm, ok.. I was just thinking about this in a really weird way and I don't
know why. What you say indeed makes total sense as does the behavior now.
Sorry for the brainfart


Jon Skeet said:
neverstill said:
ahh, very misleading.

See I thought it would start at 'h' (url[0]) and look all the way to
'x'(url[url.Length-1]
in a left to right manner, are you saying that LastIndexOf() starts at
Length-1 and goes to the start? (right to left)

weird... Thanks for the response! :)

Not weird at all - that's why it's LastIndexOf rather than IndexOf. If
you want it to look from left to right, just use IndexOf.
 
neverstill said:
wow....
uhm, ok.. I was just thinking about this in a really weird way and I don't
know why. What you say indeed makes total sense as does the behavior now.
Sorry for the brainfart

No need to apologise - it's not like we don't all do the same kind of
thing every so often. I'm just glad this one didn't last long - there's
nothing worse than a brainfart which lasts several days!
 
Back
Top