简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM