C#模式匹配 [英] C# pattern matching
问题描述
我有点新的C#中,我要寻找一个字符串匹配模式做到以下几点,
我有这样
$ B字符串$ b
这本书将在数量
$ b将出席$ b 块引用>
接收唐宁街11号
展出,并将于关键医疗
我需要创建一个span标记突出使用startIndex和长度有一定的文字片段,
有关例如,
- 的startIndex = 3,长度= 10
- 的startIndex = 8,长度= 8
块引用>
我需要动态地创建一个span标记,也可以创建一个对于交叉口单独span标签
在这种情况下,
在< ;跨度ID ='span1'颜色=蓝>本书< / SPAN> <跨度ID ='intersectionSpan'颜色粉红=>将< / SPAN> <跨度ID = SPAN2'颜色='黄色'>展出< / SPAN>
任何人遇到任何种类的设计模式或smiler问题
请指教
解决方案我不认为这德兴模式相关,但我会看什么ü问道自定义控件
正如你所知道的标签控件将呈现为一个跨度,以便开始进行,例如新的控制customlabel从它里面的标签控制和克里特功能继承它接受地点(startIndex和长度)和颜色(红,黄)
让我们说我们有控制这里面功能
私人字符串AddSpan(字符串originalString,INT []的位置,串色)
{
字符串old = originalString.Substring(位置[0] ,位置[1]);
线=中newstr的String.Format(<跨度ID ={0}颜色={1}>中,idUWant,颜色);
originalString = originalString.Replace(老,中newstr);
返回originalString;
}
的originaltext是这本书将在前台唐宁街11号展出
,将被重点医疗出席
的位置是简单的2维整数数组第一个将是开始索引和第二个将是长度,颜色参数是颜色字符串
我认为这是更好地使像一类holiding的参数的资料容器只喜欢startIndex和长度和颜色的一些属性,使之更容易阅读和维护
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
解决方案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
这篇关于C#模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!