动态字符串格式 C#
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅仅因为您使用正则表达式并不意味着您必须替换文本。
以下正则表达式:
有两个“捕获组”。然后,您可以将整个文本字符串替换为以下内容:
这应该会产生以下输出:
编辑: 下面是一些演示上述内容的 C# 代码:
Just because you're using RegEx doesn't mean you have to replace text.
The following regular expression:
Has two 'capturing groups.' You can then replace the entire text string with the following:
Which should result in the following output:
Edit: Here's some C# code which demonstrates the above:
你试过
.Insert()
检查这个。have you tried
.Insert()
check this.您是否考虑过通过将每行包装在
p
或div
标记中来创建样式并设置每行的 css 类?更容易维护和建造。
Have you considered creating a style and setting the css class of each line by wrapping each line in a
p
ordiv
tag?Easier to maintain and to construct.
最简单的方法可能是使用 string.Replace() 和 string.Split()。假设您的输入字符串是
input
(未经测试):The easiest way probably is to use
string.Replace()
andstring.Split()
. Say your input string isinput
(untested):