原始字符串
它是由3个部分组成:
如何通过C#更有效地操作字符串?
1-字(中国)
2- [IMG] PIC URL [/ IMG]
3- [网址] URL [/ URL]
4- < BR>
所以结果是一样的东西下面:
sometextsometextsometextsometextsometextsometext < br> [img] http://www.a.com/a.jpg [/ img] othertextothertextothertextothertextothextxtrtextxtrtertextxtrtertext [img] http://cbcom/a.jpg [/ img] anothertextanothertextanothertextanothertextanothertext [url] http: //def[/url]alwaystext[img]http://fgcom/a.gif[/img][img]http://denet/a.png[/img]
我想要什么
仍然由3个部分组成,但稍加改变:
1-词语
2- < IMG SRC =“PIC URL” WIDTH = “300”>
3- < A HREF = “URL”> URL </A>
我现在在做什么
//content is the source string
string[] contentArray = Regex.Split(content, @"\[img\](.+?)\[/img\]", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
StringBuilder sb= new StringBuilder();
foreach (string item in contentArray)
{
//if it is a pic url
if (item.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &
(item.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
item.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) ||
item.EndsWith(".png", StringComparison.OrdinalIgnoreCase)))
{
//convert it into a < img> link
//append to sb
}
else
{
string[] contentArray1 = Regex.Split(item, @"\[url\](.+?)\[/url\]", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (string tmpItem in ContentArray)
{
if (it is a URL)
{
//convert it into a < a> link
//append to sb
}
else //must be normal text
{
//to make a better layout
//put into a < p>
//append to sb
}
}
}
}
存在的问题
上面的代码工作,但任何更好的解决方案?使其更有效我的意思是“更好的解决方案”在这里意味着更快的速度@ _ @
那么你的解决方案是否工作?如果没有,那么它不起作用,如果它,你希望改善什么? – Servy
@Servy我认为它*确实有效,但OP想知道,如果它不能更快。 – Nolonar
@Nolonar现在还不清楚它是否有效,这就是我问过的原因,也不清楚他想如何改进它,他没有试图说明这一点。他可能希望它运行得更快,使用更少的内存,更具可读性,处理目前无法使用的输入,使用更少的代码或其他任何因素。 – Servy