C# 模式匹配
我对 C# 有点陌生,我正在寻找一个字符串匹配模式来执行以下操作,
我有一个像这样的字符串
这本书将在 唐宁街 11 号接待处 并将由重点医疗保健人员参加
我需要创建一个跨度标签来使用 startIndex 和长度突出显示一些文本片段,
例如,
- 起始索引 = 3,长度 = 10
- 起始索引 = 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,
- startIndex = 3, Length = 10
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为这与设计模式相关,但我会查看您要求的自定义控件,
因为您知道标签控件将呈现为跨度,因此开始创建新的控件自定义标签,例如从标签控件和 creta 函数继承它在它内部接受位置(startindex和length)和颜色(red,yellow)
假设我们在控件内部有这个函数,
原文是这本书将在唐宁街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
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
好吧,我将从“标签”的集合开始。这些将具有要标记的文本的开始和长度。标签还应该能够判断某个位置是否在标签中。
从那里循环遍历字符串。在每个位置添加该位置的标签数量。如果它比最后一个位置多,则开始一个新标签,因为新标签与它相交。如果小于,则结束一个跨度,因为交叉点刚刚结束。保存下一个循环的数字并重复。
应该可以做到这一点。你可能想尝试一下,因为这只是我的想法。
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.
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.
您可以使用 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.