C# pattern matching

荒凉一梦 提交于 2019-12-10 23:33:42

问题


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


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2283001/c-sharp-pattern-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!