0

I have a text file which will look like this

:Hand 1:
:Hand 2:
:Hand 4:
:Hand 3:
:Hand 3::Hand 1:
:Hand 2:
:Hand 1:
:Hand 4:
:Hand 4:
:Hand 3:
:Hand 3:
:Hand 3::Hand 4:

So now what I want to know is how do I get the maximum count of lines which contain :Hand 3:

eg at first its 2
:Hand 4:
:Hand 3:
:Hand 3::Hand 1:

then its 3
:Hand 4:
:Hand 3:
:Hand 3:
:Hand 3::Hand 4:

so i want this code to then return 3, if i then wanted :Hand 4: it will be 2 here

:Hand 1:
:Hand 4:
:Hand 4:

the double :Hand1::Hand2: is because sometimes there will be 2 hands per line in text file

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

afik there isn't any function that will do this for you automatically, which means you will have to write one.

you will need to read the lines of the file using something like File.ReadAllLines() or possibly the StreamReader ReadLine()
your choice here may depend on the size of the file and available memory etc.

you then need to loop through each line and check if it contains the text you are looking for, if it does increment a counter.

the code might look something like this:

int countHands(string handToSearch) {
  int counter = 0;
  foreach (string line in File.ReadAllLines("c:\hands.txt");
    if (line.Contains(handToSearch))
      counter++;
  Return counter;
}
0

Or you can use a StreamReader class and ReadLine() method, and count +1 on each step if line contains your requirement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.