读书人

C# 怎么查找字符串

发布时间: 2012-01-14 20:02:35 作者: rapoo

C# 如何查找字符串
现在假设有一字符串为
"text[img]http://domainname/image.jpg[/img]text "
如何找到 [img]到[/img]之间的字符串 "http://domainname/image.jpg "?
除了自己写方法外, .NET有提供相关的方法?
反正就是找个最简单的方法.

[解决办法]
string yourStr = "text[img]http://domainname/image.jpg[/img]text ";
Match results = Regex.Match(yourStr, "\\[img\\](.+?)\\[/img\\] ", RegexOptions.IgnoreCase);
Console.WriteLine(results.Groups[1].Value);
[解决办法]
string strTest= "text[img]http://domainname/image.jpg[/img]text ";

int intFront=strTest.LastIndexOf( "[img] ")+5;
int intBack=strTest.LastIndexOf( "[/img] ");
int intLength=strTest.Length;

int intStrLength=intLength-intFront-(intLength-intBack);

string strResult=strTest.Substring(intFront,intStrLength);

Response.Write(strResult);

读书人网 >C#

热点推荐