首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# 查寻字符串

2012-08-30 
c# 查找字符串我现在在做一个根据关键字,查找关键内容的功能。页面有三个控件,两个text,一个btton。当我输入

c# 查找字符串
我现在在做一个根据关键字,查找关键内容的功能。页面有三个控件,两个text,一个btton。当我输入关键字,输入网址时,会读取这个页面的html,我现在想把这个页面中,关键字前面的内容找出来,如下:
我输入关键字:豆油上涨步伐或将放缓
读取的页面中包含这样的一段字符:<A title="豆油上涨步伐或将放缓" href="zixun/InfoList.asp?id=9164" target=_blank>豆油上涨步伐或将放缓</A>
我现在想根据我输入的关键字把zixun/InfoList.asp?id=9164取出来,请问那位有思路,帮我想一下。谢谢了

[解决办法]

C# code
void Main(){      string s="<A title=\"豆油上涨步伐或将放缓\" href=\"zixun/InfoList.asp?id=9164\" target=_blank>豆油上涨步伐或将放缓</A>";Match m=Regex.Match(s,@"(?is)<a\stitle=""豆油上涨步伐或将放缓""\shref=""([^>].*?)""\s.*?>.*?</a>");if(m.Success){Console.WriteLine(m.Groups[1].Value);}}//zixun/InfoList.asp?id=9164
[解决办法]
先添加一个webbrowser控件
C# code
 public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();            this.webBrowser1.Navigate(textBox1.Text);        }        private void button1_Click(object sender, EventArgs e)        {            if (webBrowser1.Document != null)            {                HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("a");                foreach (HtmlElement elem in elems)                {                    String nameStr = elem.GetAttribute("title");                    if (nameStr == textBox2.Text)                    {                        string hrefStr = elem.GetAttribute("href");                        MessageBox.Show(hrefStr);                        break;                    }                }            }            else                MessageBox.Show("网页加载出错");        }    } 

热点排行