使用streamreader查找字符串的有效方法 [英] efficient way to find string with streamreader

查看:55
本文介绍了使用streamreader查找字符串的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到Web响应,并使用streamreader以字符串形式获取响应

I get web response and use streamreader to obtain the response as a string

我的代码是

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string strResponse = reader.ReadToEnd();

字符串样本为

<div class="box-round">
<ol  style="list-style-type: decimal;list-style-position:outside;margin-left:42px;">
<li>Order ID #A123456 already exists: Update performed
</ol>
</div>

<div class="box-round">
    <ol  style="list-style-type: decimal;list-style-position:outside;margin-left:42px;">
    <li>New order created
    </ol>
</div>

我想在字符串中找到以下行

I want to locate the following line within the string

Order ID #A123456 already exists: Update performed

New order created

这是搜索行的最佳方法

  while (!reader.EndOfStream)
    {
        line = reader.ReadLine();
        if (!string.IsNullOrEmpty(line))
        {

        }
    }

推荐答案

好吧,我个人将使用:

string line;

while ((line = reader.ReadLine()) != null)
{
    if (line.Contains(...))
    {
    }
}

读取该行会为您提供数据会告诉您是否已到达流的末尾.我同意Jeff的观点-通过逐行阅读来解析" HTML通常是一个坏主意.当然,在您的特定情况下 就足够了.

Reading the line gives you the data and tells you whether you've reached the end of the stream. I agree with Jeff though - "parsing" HTML by reading it line by line is generally a bad idea. It may be good enough in your specific situation, of course.

这篇关于使用streamreader查找字符串的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆