5

I need some help. I'm writing an error log using text file with exception details. With that I want my stack trace details to be written like the below and not in straight line to avoid the user from scrolling the scroll bar of the note pad or let's say on the 100th character the strings will be written to the next line. I don't know how to achieve that. Thanks in advance.

SAMPLE(THIS IS MY CURRENT OUTPUT ALL IN STRAIGHT LINE)

STACKTRACE:

at stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk

**MY DESIRED OUTPUT (the string will write to the next line after certain character count)

STACKTRACE:

at stacktraceabcdefghijklmno    
pqrstuvwxyztacktraceabcdefgh    
ijklmnopqrswxyztacktraceabcd    
efghijk

MY CODE

builder.Append(String.Format("STACKTRACE:"));
            builder.AppendLine();
            builder.Append(logDetails.StackTrace);  
user2771704
  • 5,994
  • 6
  • 37
  • 38
Moccassin
  • 169
  • 1
  • 4
  • 15
  • **[This](http://stackoverflow.com/questions/7768373/c-sharp-line-break-every-n-characters)** should help. – user2771704 Oct 24 '13 at 07:53
  • Does this answer your question? [Splitting a string / number every Nth Character / Number?](https://stackoverflow.com/questions/4133377/splitting-a-string-number-every-nth-character-number) – StayOnTarget Sep 26 '22 at 14:55

4 Answers4

11

Following example splits 10 characters per line, you can change as you like {N} where N can be any number.

var input = "stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk";
var regex = new Regex(@".{10}");
string result = regex.Replace(input, "$&" + Environment.NewLine);
Console.WriteLine(result);

Here is the Demo

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

you can use the following code:

string yourstring;

StringBuilder sb = new StringBuilder();

for(int i=0;i<yourstring.length;++i){
if(i%100==0){
sb.AppendLine();
}
sb.Append(yourstring[i]);
}
André Leal
  • 186
  • 1
  • 8
  • Thanks your code also works. But I prefer to minimize loops in my code. Thanks again for your answer. Very much appreciated. :) – Moccassin Oct 24 '13 at 08:07
1

you may create a function for this

    string splitat(string line, int charcount)
{
     string toren = "";
     if (charcount>=line.Length)
     {
          return line;
     }
     int totalchars = line.Length;
     int loopcnt = totalchars / charcount;
     int appended = 0;
     for (int i = 0; i < loopcnt; i++)
     {
          toren += line.Substring(appended, charcount) + Environment.NewLine;
          appended += charcount;
          int left = totalchars - appended;
          if (left>0)
          {
               if (left>charcount)
               {
                    continue;
               }
               else
               {
                    toren += line.Substring(appended, left) + Environment.NewLine;
               }
          }
     }
     return toren;
}
Ratna
  • 2,289
  • 3
  • 26
  • 50
1

Best , Easiest and Generic Answer :). Just set the value of splitAt to the that number of character count after that u want it to break.

string originalString = "1111222233334444";
List<string> test = new List<string>();
int splitAt = 4; // change 4 with the size of strings you want.
for (int i = 0; i < originalString.Length; i = i + splitAt)
{
    if (originalString.Length - i >= splitAt)
        test.Add(originalString.Substring(i, splitAt));
    else
        test.Add(originalString.Substring(i,((originalString.Length - i))));
}
Sandeep Kushwah
  • 590
  • 1
  • 18
  • 35