0

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.

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by behrooz.09
0

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

0

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();
        }
    }
}
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.