Split string after certain character count

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);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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]);
}

Method 3

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;
}

Method 4

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))));
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x