Is it possible to iterate over the contents of a text field? If not, what is the best strategy for reading in turn?

I am designing a crawler that will get certain content from a web page (using either string manipulation or regular expression).

I can get the contents of the webpage as a response stream (using the entire httpwebrequest article), and then for testing / dev purposes, I write the contents of the stream in a multi-line text box in my ASP.NET webpage.

Is it possible for me to go through the contents of the text field and then say: β€œIf textbox1.text.contains (or save the text of the text field as a string variable), a particular line then increments the counter.” The problem with the text field is that the line loses formatting, so in one long line without interrupting the line. Can it be changed?

I would like to do this, rather than writing the contents to a file, because writing to a file means that I will have to handle all kinds of external problems. Of course, if this is the only way, then so be it. If I have to write to a file, then what is the best strategy for scrolling through each line (I'm a bit overloaded and thus confused, since there are a lot of logical and language methods to use) looking for a condition? Therefore, if I want to search for the string "Hello" in the following text:

My name is xyz I'm xyz yo Hi blah blah blah Bye

When I get hello, I want to increment an integer variable.

Thank,

+3
source share
6 answers

In my opinion, you can separate the contents of the text with words instead of lines:

public int CountOccurences(string searchString)
{
    int i;
    var words = txtBox.Text.Split(" ");

    foreach (var s in words)
        if (s.Contains(searchString))
           i++;

    return i;
}

No need to keep line breaks if I understand your purpose correctly.

, .

+1

, , :)

string template = txtTemplate.Text;
            string[] lines = template.Split(Environment.NewLine.ToCharArray());
0

.

HTML- ( Microsoft, HTML-). , ?

, ( ), , .

0

, , , (, , ..), , . , , ​​, .

"/ ", () .

0

? - , . , :

  Stream response = webRequest.GetResponse().GetResponseStream();
  StreamReader reader = new StreamReader(response);
  String line = null;

  while ( line = reader.ReadLine() ) 
  {
    if (line.Contains("hello"))
    {
      // increment your counter
    }
  }

if , , :).

, , .

0

html. , , - - , , . , , , . , , .

while . ? readline, , .

I need to save the string as a var string, where a specific string is found, since I will need to perform some operations (and some part of the string), so I always looked at readline.

Thank!

0
source

Source: https://habr.com/ru/post/1698473/


All Articles