> I want to split a big string by nymber of characters. for eg., if total
length of the string is 80 and I want to split by each 10 characters and get
8 lines of output.
I written a function but still I search for the optimized one because the
strings I receive will be long. Is there are any special classes or I can use
regular expressions?
I don't believe there are any classes designed specifically for this.
Just don't code the function as
while (Input != "")
{
int ThisLength = Math.Max(TargetLength, Input.Length);
Output.Add(Input.Substring(0, ThisLength));
Input = Input.Remove(0, ThisLength);
// all this 'popping' is VERY expensive on big strings
}
instead, move a LeftPtr along, and Substring() from that.
Alternatively, you could construct a regex
string Pattern = String.Format(".{1,{{0}}}", TargetLength)
// or
string Pattern = ".{1," + TargetLength.ToString() + "}";
// in either case, be sure to use RegexOptions.Singleline
and construct your output from the Matches collection (or by calling
Match / NextMatch). Might be a bit less code than the LeftPtr
alternative, and probably about as performant.
--
www.midnightbeach.com