本文介绍了LastIndexOf()奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string url =

k = url.LastIndexOf(" /,0,url.Length);


抛出异常:Count必须为正数,count必须引用string / array / collection中的

位置。参数名称:计数


为什么?这对我来说似乎完全合法......但它失败了吗?有人看到了

的问题吗?


-SK

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

推荐答案






是的 - 你要求它开始检查第一个角色,并且

向后看url.Length角色位置。它显然不能这样做,因为它只能看到第一个位置然后停止。


不幸的是,这两个都没有。文档和错误消息

是准确的 - 它们基本上只对IndexOf而不是

LastIndexOf准确。实际上,数量必须小于或等于

(startIndex + 1)。


-

Jon Skeet - < sk *** @ pobox.com>


如果回复群组,请不要给我发邮件



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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





这篇关于LastIndexOf()奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:35