动态字符串格式 C#

发布于 2024-10-27 00:32:44 字数 961 浏览 7 评论 0原文

在 C#、Windows 窗体中,我将如何完成此操作:


07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>

因此,如您所见,我有一个返回字符串,该字符串可能相当长,但我希望能够将数据格式化为如下所示:


<b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/>

正如您可以的看,我本质上想在标题文本前面添加 。 time,并在 : 部分之前附加

所以是的,哈哈,我有点迷路了。

我已经搞乱了 .Replace()Regex 模式,但没有取得太大成功。我真的不想替换文本,只是在某些位置附加/预先添加。

有没有简单的方法可以做到这一点?

注意:[]标签实际上是<>标签,但我不能在这里使用它们,哈哈

In C#, Windows Form, how would I accomplish this:


07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>

So, as you can see, i have a return string, that can be rather long, but i want to be able to format the data to be something like this:


<b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/>

As you can see, i essentially want to prepend <b><font color="Red"> to the front of the header text & time, and append </font></b> right before the : section.

So yeah lol i'm kinda lost.

I have messed around with .Replace() and Regex patterns, but not with much success. I dont really want to REPLACE text, just append/pre-pend at certain positions.

Is there an easy way to do this?

Note: the [] tags are actually <> tags, but i can't use them here lol

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-11-03 00:32:44

仅仅因为您使用正则表达式并不意味着您必须替换文本。

以下正则表达式:

(\d+:\d+.*?:)(\s.*?\[br/\])

有两个“捕获组”。然后,您可以将整个文本字符串替换为以下内容:

[b][font color="Red"]\1[/font][/b]\2

这应该会产生以下输出:

[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]

编辑: 下面是一些演示上述内容的 C# 代码:

var fixMe = @"07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]";
var regex = new Regex(@"(\d+:\d+.*?:)(\s.*?\[br/\])");
var matches = regex.Matches(fixMe);

var prepend = @"[b][font color=""Red""]";
var append = @"[/font][/b]";

string outputString = "";
foreach (Match match in matches)
{
    outputString += prepend + match.Groups[1] + append + match.Groups[2] + Environment.NewLine;
}

Console.Out.WriteLine(outputString);

Just because you're using RegEx doesn't mean you have to replace text.

The following regular expression:

(\d+:\d+.*?:)(\s.*?\[br/\])

Has two 'capturing groups.' You can then replace the entire text string with the following:

[b][font color="Red"]\1[/font][/b]\2

Which should result in the following output:

[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]

Edit: Here's some C# code which demonstrates the above:

var fixMe = @"07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]";
var regex = new Regex(@"(\d+:\d+.*?:)(\s.*?\[br/\])");
var matches = regex.Matches(fixMe);

var prepend = @"[b][font color=""Red""]";
var append = @"[/font][/b]";

string outputString = "";
foreach (Match match in matches)
{
    outputString += prepend + match.Groups[1] + append + match.Groups[2] + Environment.NewLine;
}

Console.Out.WriteLine(outputString);
路还长,别太狂 2024-11-03 00:32:44

你试过.Insert()检查这个

have you tried .Insert() check this.

千仐 2024-11-03 00:32:44

您是否考虑过通过将每行包装在 pdiv 标记中来创建样式并设置每行的 css 类?

更容易维护和建造。

Have you considered creating a style and setting the css class of each line by wrapping each line in a p or div tag?

Easier to maintain and to construct.

孤芳又自赏 2024-11-03 00:32:44

最简单的方法可能是使用 string.Replace() 和 string.Split()。假设您的输入字符串是 input (未经测试):

var output = string.Join("<br/>", in
    .Split("<br/>)
    .Select(l => "<b><font color=\"Red\">" + l.Replace(": ", "</font></b>: "))
    .ToList()
    ) + "<br/>";

The easiest way probably is to use string.Replace() and string.Split(). Say your input string is input (untested):

var output = string.Join("<br/>", in
    .Split("<br/>)
    .Select(l => "<b><font color=\"Red\">" + l.Replace(": ", "</font></b>: "))
    .ToList()
    ) + "<br/>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文