Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All

Why does this little piece of code throw and exception?

C#
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);


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
Posted
Comments
Richard C Bishop 30-Oct-13 10:44am    
Try removing the StringBuilder and both ints. You don't need that. See my solution below.
mzrax 30-Oct-13 10:46am    
I'm sorry but I don't see your solution. What do you mean?
mzrax 30-Oct-13 10:50am    
I've tried this and I still get the same exception.

string iText1 = xText1.Substring(0, 23);
string iText2 = xText1.Substring(24, xText1.Length);
Richard C Bishop 30-Oct-13 10:52am    
Sorry, it took me a few to get it posted.
mzrax 30-Oct-13 11:03am    
When I run the code in Console mode it works fine and I can put in any string. So why is it in my program I am now getting the same exception but now it says Length cannot be less than zero. Parameter name: length

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));
 
Share this answer
 
Did you try:
int xMax = iMax - 23 - 1;
Index number is zero-based, Length is not.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900