efficient way to find string with streamreader

前端 未结 3 1566
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 10:57

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

my code is

HttpWebResponse response = (HttpWebResponse) request.GetRespons         


        
相关标签:
3条回答
  • 2021-01-12 11:36

    Well, personally I would use:

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

    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.

    0 讨论(0)
  • 2021-01-12 11:38

    This really depends - do you need to know where in the DOM your particular text resides? How large is the input? Will your string ever be split between two lines?

    If you are only concerned about the presence of the text and your input is small enough to reside in memory, I'd just read the entire thing in to memory. I'm not sure what the exact algorithm the CLR uses to do string matching, but some of the faster routines involve pre-processing both the query and the string to be searched, and having more information for the pre-processing could potentially yield a faster search.

    Of course, this all depends on CLR internals and your particular requirements - test, test, test.

    If you want to capture more information about your text and its relation to the surrounding document, I'd suggest looking at the HtmlAgility library for parsing your document.

    0 讨论(0)
  • 2021-01-12 11:43

    Here's how to do it with regex, sure regex isn't the best method, but if this is a one time thing working with an html parser is probably more than you are bargaining for

    Match myMatch = Regex.Match(input, 
           @"<div class=""box-round"">.*?<li>(.*?)</ol>", Regex.Singleline);
    
    if (myMatch.Success)
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题