简体   繁体   English

在某些字符计数后拆分字符串

[英]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. 有了这个我想要我的堆栈跟踪细节写如下,而不是直线,以避免用户滚动记事本的滚动条或让我们说在第100个字符,字符串将写入下一行。 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. 下面的示例每行分割10个字符,您可以根据需要更改{N} ,其中N可以是任意数字。

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

Here is the Demo 这是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. 只需将splitAt的值设置为您希望它中断后的字符数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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