Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);  
like image 835
Moccassin Avatar asked Dec 11 '22 10:12

Moccassin


1 Answers

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

like image 195
Sriram Sakthivel Avatar answered Jan 05 '23 23:01

Sriram Sakthivel