0

Possible Duplicate:
Splitting a string into chunks of a certain size

Is there anyway to split a string for the every 714 character? this is due to the debug.writeline limitation in c#

Community
  • 1
  • 1
ericlee
  • 2,703
  • 11
  • 43
  • 68
  • 2
    Yes there is - [what have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Justin Dec 15 '11 at 16:31
  • and there is no limitation if you are doing .WriteLine() to a log file perhaps you may what to consider that as well.. but would need to see what code you have thus far.. – MethodMan Dec 15 '11 at 16:32

4 Answers4

4

Yes, I'd use Substring:

string myString = "Some very long string you need to output.";

for(int i = 0; i < myString.length; i += 714) {
    Debug.WriteLine(myString.Substring(i, Math.Min(714, myString.Length - i)));
}

Or, for a fancy one-liner:

foreach(var s in Regex.Matches(myString, ".{,714}")) Debug.WriteLine(s.Value);
Ry-
  • 218,210
  • 55
  • 464
  • 476
1

You could use this code:

static IEnumerable<string> Split(string str, int chunkSize)
{
    return Enumerable.Range(0, str.Length / chunkSize)
        .Select(i => str.Substring(i * chunkSize, chunkSize));
}

From Splitting a string into chunks of a certain size

And make your own writeline method

Community
  • 1
  • 1
Tremmors
  • 2,906
  • 17
  • 13
0

Solved by using this

        int chunkSize = 700;
           int stringLength = str.Length;
           for (int i = 0; i < stringLength; i += chunkSize)
           {
               if (i + chunkSize > stringLength) chunkSize = stringLength - i;
               Debug.WriteLine(str.Substring(i, chunkSize));

           }
ericlee
  • 2,703
  • 11
  • 43
  • 68
0

Here's an extension method that should do the trick (sans LINQ just to keep things clear):

public static string[] SplitLength(this string val, int length)
{
    var parts = new List<string>();

    for(int i = 0; i < val.Length; i += length)
    {
        parts.Add(val.Substring(i * length, length));
    }

    return parts.ToArray();
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536