Hello guys,
I have a text file which I have to read it from my code which is Ok.
THE PROBLEM :
the program has to read the file and just take and extract or print some part of the text file.
it means Strings between 2 Words.

Here is the content of the text file:
// #Name
// Behrooz 123_box_2000
// ...
// ...
// .....
// #Requirements
// lab_121, lab_222, lab_312
// ...
// ...
// .....
// #Name
// John 555_box_2010
// ...
// ...
// .....
// #Requirements
// lab_666, lab_819, lab_731

AND I need to print out just :

// #Name
// Behrooz 123_box_2000
// #Requirements
// lab_121, lab_222, lab_312

// #Name
// John 555_box_2010
// #Requirements
// lab_666, lab_819, lab_731


I appreciated if you can help me.
BR,
Behrooz.

Recommended Answers

All 2 Replies

This is a pretty easy task. What have you tried so far? We don't do your assignments for you.

I have done this by myself and I didn't get any result! :(

namespace readAndSearch
{
    class read_Search_2
    {
        public static void Main()
        {
            string tagStart = "// #NAME";
            string tagEnd = "// #COUNTER";
            StreamReader myFile = new StreamReader(@"test.txt");
            string myString = myFile.ReadToEnd();

            string[] array = myString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int start = (Array.IndexOf(array, tagStart)) + 1; //+1 means that we get rid of #NAME line
            int end = 0;

            for (int i = start; i < myString.Length; i++)
            {
                string item = array[i];
                if (item == tagEnd)
                {
                    end = i;
                    break;
                }
            }


            string result = null;
            for (int i = start; i < end; i++)
                result += array[i] + " ";
            Console.ReadKey();
        }
    }
}
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.