Q:

Unable to extract a substring from a string

I am long string array and i want to pass it to another function in the chunks of 250 characters one time, i have written this code:
var cStart = 0;
var phase = 250;
var cEnd = cStart + phase;
var count = 0;

while (count < 10000)
{
    string fileInStringTemp = "";
    fileInStringTemp = fileInString.Substring(cStart, cEnd);
    var lngth = fileInStringTemp.Length;

    //Do Some Work

    cStart += phase;
    cEnd += phase;
    count++;
}

In the first iteration of the loop the value of lngth is 250 which is fine, in the next iteration i also want it to 250 because i am extracting substring from 250-500 characters but shockingly the value of lngth variable in the second iteration gets 500.
Why is that? i am also trying to initialize string variable everytime in the loop so it starts from zero but no gain.

A:

Substring's second parameter is the length you want, not the stop index.
public string Substring(
    int startIndex,
    int length
)

So, all you need to do is change your code to have the start index and length (phase)
fileInString.Substring(cStart, phase)

A:

Here is the MSDN link about how to work with Substring:
https://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx
According to MSDN first parameter in Substring method is StartIndex which is defined as The zero-based starting character position of a substring and second parameter is used to define lenght of substring which is defined as The number of characters in the substring.
So you should try this:
var cStart = 0;
var phase = 250;
var count = 0;

while (count < 10000)
{
    string fileInStringTemp = "";
    fileInStringTemp = fileInString.Substring(cStart, phase);
    var lngth = fileInStringTemp.Length;

    //Do Some Work
    count++;
    cStart = phase * count + 1;
}

