C# pattern matching
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 &开发者_如何学Golt;/ 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 )
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
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
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.
精彩评论