经过一定的字符数字符串分割 [英] Split string after certain character count

查看:167
本文介绍了经过一定的字符数字符串分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。我正在写使用文本文件异常详细信息错误日志。有了,我要被写入我的堆栈跟踪细节,如下面,而不是在直线上,以避免用户从滚动记事本的滚动条或让我们在第100字符的字符串将被写入到下一行说。我不知道该如何实现这个目标。先谢谢了。

样品(这是我目前将所有数据输出直线)

 堆栈跟踪:在stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk

* *我需要的输出(该字符串将写信给某些后字符数下一行)

 堆栈跟踪:在stacktraceabcdefghijklmno
pqrstuvwxyztacktraceabcdefgh
ijklmnopqrswxyztacktraceabcd
efghijk

我的code

  builder.Append(的String.Format(堆栈跟踪));
            builder.AppendLine();
            builder.Append(logDetails.StackTrace);


解决方案

以下例子将每行10个字符,你可以为你改变,比如 {N} ,其中n可以是任何数目

  VAR输入=stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk;
VAR正则表达式=新的正则表达式(@{10});
字符串结果= regex.Replace(输入$&安培;+ Environment.NewLine);
Console.WriteLine(结果);

下面是演示

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

这篇关于经过一定的字符数字符串分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆