简体   繁体   English

C#模式匹配

[英]C# pattern matching

I am bit new to c#, i am looking for a string matching pattern to do the following, 我对C#有点陌生,我正在寻找一个字符串匹配模式来执行以下操作,

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 该书将在唐宁街11号的招待会上展出,主要医疗保健人员将参加

i need to create a span tag to highlight some text fragments using startIndex and length, 我需要创建一个span标签以使用startIndex和length突出显示一些文本片段,

for an example, 举个例子

  1. startIndex = 3, Length = 10 startIndex = 3,长度= 10
  2. startIndex = 8, Length = 8 startIndex = 8,长度= 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 请指教

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 ) 如您所知,标签控件将呈现为跨度,因此开始制作新的控件customlabel,例如从标签控件和creta函数继承它,以接受位置(startindex和length)和颜色(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 原文为《这本书》将在唐宁街11号的招待会上展出,并将有主要的医疗人员参加

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 该位置是简单的二维整数数组,第一个是起始索引,第二个是length,color参数是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 我认为最好为像这样的类的参数制作数据容器,使其只隐藏一些属性,如startindex,length和color,以使其更易于阅读和维护

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. 您可能想尝试一下它,因为这只是我的脑袋。

you can use the IndexOf 您可以使用IndexOf

this link helps you: http://msdn.microsoft.com/en-us/library/ms228630%28VS.80%29.aspx 该链接可以帮助您: http : //msdn.microsoft.com/zh-cn/library/ms228630%28VS.80%29.aspx

and if you have the startIndex and length; 并且如果您具有startIndex和length; you can use the substring simply to get the string you want to inject in the span tag. 您可以仅使用子字符串来获取要注入到span标签中的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM