views:

374

answers:

3

Hi guys,

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

A: 

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.

Craig
+1  A: 

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

Amgad Fahmi
Because strings are a reference type in .NET anything outside AddSpan won't see the result of the replace. See here for an explanation: http://www.yoda.arachsys.com/csharp/parameters.html
Alex Peck
yeah i know i just gave her the key and it should modify it like make the function return the string or let the function modify a public property or the label text directly , thnx anyway
Amgad Fahmi
A: 

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.

Ahmad