SubString问题ArgumentOutOfRangeException未处理 [英] SubString Problem ArgumentOutOfRangeException was unhandled

查看:165
本文介绍了SubString问题ArgumentOutOfRangeException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All



为什么这一小段代码会抛出异常?



Hello All

Why does this little piece of code throw and exception?

string xText1 = "I love to party but why doesn't this work?";
StringBuilder sb1 = new StringBuilder(xText1);
int iMax = sb1.Length;
int xMax = iMax - 23;
string iText1 = xText1.Substring(0, 23);
string iText2 = xText1.Substring(24, xMax);





我正在尝试将字符串拆分为仅占用前24个字符,然后取出字符串的剩余部分并使用它做其他事情。 />


当这个代码运行时,xMax会给出一个数字但是当我尝试将它与子字符串一起使用时会引发异常。 iText1占用前24个字符,但为什么iText2不占用其余字符?



它会抛出这个:

ArgumentOutOfRangeException未处理。

索引和长度必须引用字符串中的位置。

参数名称:长度



任何帮助都会非常感谢。



谢谢



Mike



I'm trying to split the string to only take the first 24 characters and then take the remaining part of the string and do something else with it.

When this runs through the code xMax is given a number but when I try and use it with the substring it throws an exception. iText1 takes the first 24 characters but why doesn't iText2 take the remaining characters?

It throws this:
ArgumentOutOfRangeException was unhandled.
Index and length must refer to a location within the string.
Parameter name: length

Any help would be much appreciated.

Thanks

Mike

推荐答案

试试这个:

Try this:
string xText1 = "I love to party but why doesn't this work?";
string iText1 = xText1.Substring(0, 23);
string iText2 = xText1.Substring(24, (xText1.Length - iText1.Length - 1));


您尝试过:

int xMax = iMax - 23 - 1;

索引号从零开始,长度不是。
Did you try:
int xMax = iMax - 23 - 1;
Index number is zero-based, Length is not.


因为你做错了。 :-)首先,你需要使用调试器并用常规字符串比较你的长度和位置。



你不应该硬编码索引,长度的位置。无论值是什么,您总是可以编写这样的输入字符串,这会使您的代码无效并抛出异常。但你做得更糟。你硬编码那些立即常数三次,23和24中的两个(使用0通常很好,但也可以查看)。想想当你改变其中一个时会发生什么,你怎么去找出修改代码的地方?这种方法根本不可接受。



写什么?我会告诉你我是否知道你的目标。通常,您需要某些确定的东西,例如,从字符串中提取一些特征。还要记住:合成字符串总是更好,而不是拆分/解析它们。您需要根据最终目标而不是中间目标来检查您的方法。



因此,完全重写此代码并使用调试器。此外,不要忘记索引是从零开始的。



-SA
Because you are doing it wrong. :-) First of all, you need to use the debugger and compare you lengths and positions with regular string.

You should not hard-code indices, position of lengths. No matter what is the value, you can always write such input string which would make your code invalid and throw an exception. But you did it even worse. You hard-coded those immediate constants three times, two of 23 and 24 (using 0 is usually fine, but also can be reviewed). Think what happens when you change one of them, how are you going to find out where else to modify the code? This approach is simply not acceptable.

What to write instead? I would tell you if I knew your goal. Usually, you want something certain, for example, extract some feature from the string. Also remember: it's always better to synthesize strings, not split/parse them. You need to review your approach according to the ultimate, not intermediate goals.

So, rewrite this code completely and use the debugger. Also, don't forget that indexing is zero-based.

—SA


这篇关于SubString问题ArgumentOutOfRangeException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆