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

Recommended Answers

All 2 Replies

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;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.