out of range exception (except it isn't)
i'm using visual studio 2010 and i'm getting the error:
Unhandled Exception: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
Occuring on a call to Substring(int,int);
here's my code (it's testing so there are magic numbers):
Code:
//No out of bounds exceptions
int temp = end - 150;
if (temp > 0)
{
source = source.Substring(temp, end);
}
source is approximately 32000 characters in length and end is looking at index ~18000 (and obviously temp is index less 150). I have tested it with the parameters such as:
Code:
source = source.Substring(0,end);
and there is no problem, but i have no idea why it would not work with temp. i have tried using a dummy string variable, but as i figured it has nothing to do with the assignment source = source.Substring...
No idea how this can be the case >_>
EDIT: nevermind, i misremembered Substring (second parameter is the number of characters you want to copy).
Re: out of range exception (except it isn't)
since i apparently can't edit my post, i do want to say that there was STILL an error when using 0 as a parameter, not that there wasn't.
Re: out of range exception (except it isn't)
So does substring start at 0? I am acustomed to string functions starting at 1 ??
Re: out of range exception (except it isn't)
Quote:
Originally Posted by
DataMiser
So does substring start at 0? I am acustomed to string functions starting at 1 ??
In c# the string index starts at 0.
Re: out of range exception (except it isn't)
Read up on the substring paarameters in msdn.
The 1st param is the starting index. The 2nd param is the lenght of the string to extract.
Re: out of range exception (except it isn't)
What if the end excedes lenght of the string?
Code:
source = source.Substring(0, Math.Min(end, source.Length));