C# 模式匹配

发布于 2024-08-21 21:45:47 字数 623 浏览 10 评论 0原文

我对 C# 有点陌生,我正在寻找一个字符串匹配模式来执行以下操作,

我有一个像这样的字符串

这本书将在 唐宁街 11 号接待处 并将由重点医疗保健人员参加

我需要创建一个跨度标签来使用 startIndex 和长度突出显示一些文本片段,

例如,

  1. 起始索引 = 3,长度 = 10
  2. 起始索引 = 8,长度 = 8

我需要动态创建一个跨度标签,并为交叉点创建一个单独的跨度标签

,在这种情况下,

The < span id= 'span1' color='blue'> book < /span> < span id='intersectionSpan' color= pink > will </ span> < span id '= span2' color = 'yellow' > be showcased </ span>

任何人都遇到过任何类型的设计模式或笑脸问题,

请建议

I am bit new to c#, i am looking for a string matching pattern to do the following,

I have a string like this

The book will be showcased at a
reception in Number 11 Downing Street
and will be attended by key healthcare

i need to create a span tag to highlight some text fragments using startIndex and length,

for an example,

  1. startIndex = 3, Length = 10
  2. startIndex = 8, Length = 8

i need to create a span tag dynamically and also create a separate span tag for intersections

in this case,

The < span id= 'span1' color='blue'> book < /span> < span id='intersectionSpan' color= pink > will </ span> < span id '= span2' color = 'yellow' > be showcased </ span>

anyone has come across any kinds of design pattern or smiler problems

please advice

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

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

发布评论

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

评论(3

薆情海 2024-08-28 21:45:47

我不认为这与设计模式相关,但我会查看您要求的自定义控件,

因为您知道标签控件将呈现为跨度,因此开始创建新的控件自定义标签,例如从标签控件和 creta 函数继承它在它内部接受位置(startindex和length)和颜色(red,yellow)

假设我们在控件内部有这个函数,

 private string AddSpan(string originalString, int[] location, string color)
    {
        string old = originalString.Substring(location[0], location[1]);
        string newStr = string.Format("<span id= '{0}' color='{1}'>", "idUWant", color);
        originalString = originalString.Replace(old, newStr);
        return originalString ; 
    }

原文是这本书将在唐宁街11号的接待处展示
并将由关键医疗保健人员参与,

该位置是简单的二维整数数组,第一个将是起始索引,第二个将是长度,颜色参数是颜色字符串

我认为最好为参数创建数据容器,例如类仅保留一些属性,例如起始索引、长度和颜色,以便于阅读和维护

I don't think this related to desing pattern but i would look to what u asked as custom control

as you know the label control will render as a span so start to make new control customlabel for example inherit it from the label control and creta functions inside it to accept the locations (startindex and length ) and the color (red , yellow )

let's say we have this function inside the control

 private string AddSpan(string originalString, int[] location, string color)
    {
        string old = originalString.Substring(location[0], location[1]);
        string newStr = string.Format("<span id= '{0}' color='{1}'>", "idUWant", color);
        originalString = originalString.Replace(old, newStr);
        return originalString ; 
    }

the originaltext is The book will be showcased at a reception in Number 11 Downing Street
and will be attended by key healthcare

the location is simple 2 dimension array of integer the first one will be the start index and second one will be length , color parameter is color string

i think it's better to make data container for the paramters like a class holiding only a few properties like startindex and length and color to make it easier for reading and maintaining

ゞ花落谁相伴 2024-08-28 21:45:47

好吧,我将从“标签”的集合开始。这些将具有要标记的文本的开始和长度。标签还应该能够判断某个位置是否在标签中。

bool IsInTag(int position)

从那里循环遍历字符串。在每个位置添加该位置的标签数量。如果它比最后一个位置多,则开始一个新标签,因为新标签与它相交。如果小于,则结束一个跨度,因为交叉点刚刚结束。保存下一个循环的数字并重复。

应该可以做到这一点。你可能想尝试一下,因为这只是我的想法。

Well, I would start with a collection of "tags". These will have the start and length of the text to tag. The tag should also be able to tell if a certain position is in the tag.

bool IsInTag(int position)

From there just loop through the string. At each position add up the number of tags at that position. If it is more than the last position, start a new tag because a new tag intersected it. If it is less, end a span since the intersection just ended. Save the number for the next loop and repeat.

That should do it. You may want to play around with it since this was just off the top of my head.

夏の忆 2024-08-28 21:45:47

您可以使用 IndexOf

此链接可以帮助您:
http://msdn.microsoft.com/en- us/library/ms228630%28VS.80%29.aspx

并且如果您有 startIndex 和长度;您可以简单地使用子字符串来获取要注入到 span 标记中的字符串。

you can use the IndexOf

this link helps you:
http://msdn.microsoft.com/en-us/library/ms228630%28VS.80%29.aspx

and if you have the startIndex and length; you can use the substring simply to get the string you want to inject in the span tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文