Fahid Nadeem
Fahid Nadeem

Reputation: 412

How to extract a specific text from text file in c#

enter image description hereBasically I have a text file in which there is a title and description. I want to extract the title in a specific text box and all the description in other. I have tried this code:

protected void FillForm(object sender, EventArgs e)
{
    string inputString;
    textBoxContents.Text = "";
    using (StreamReader streamReader = File.OpenText(@"E:\file.txt"))
    {
        inputString = streamReader.ReadLine();

        while (inputString != null)
        {
            textBoxContents.Text += inputString + "<br />";
            inputString = streamReader.ReadLine();
        }
    }
}

What I get is the all content of file but I want a chunk of text to appear in the textbox from this file.

Upvotes: 2

Views: 8964

Answers (3)

Sinatr
Sinatr

Reputation: 22008

How to extract a specific text from text file

What is specific? It is the most important thing to know. Knowing what makes that text specific is the thing, which allow you to write a peace of code what will do the right job.

Typically, there are:

  • Line, containing (starting from, ending with) specific characters and variants (line before that, line after that, text in line before that, etc).
  • Line number N.
  • Something else (tell us).

Knowing that your code is almost correct:

protected void FillForm(object sender, EventArgs e)
{
    textBoxContents.Text = "";
    using (var streamReader = File.OpenText(@"E:\file.txt"))
    {
        string inputString = null;
        int lineNumber;
        do
        {
            inputString = streamReader.ReadLine();
            lineNumber++;
            // this
            // get line number 7
            if(lineNumber == 7)
            {
                 textBoxContents.Text += inputString + "<br />";
                 break;
            }
            // or perhaps this
            // get next line after line containing "Description"
            if(inputString.Contains("Description"))
            {
                 inputString = streamReader.ReadLine();
                 textBoxContents.Text += inputString + "<br />";
                 break;
            }
        } while (inputString != null)
    }
}

P.S.: a tip, don't make event handler a method. That means

void ButtonClick(object sender, EventArgs e)
{
    FillForm();
}

Upvotes: 0

TedEwen
TedEwen

Reputation: 38

I have a text file in which there is a title and description....

Seems like a description of the file format to me ;)

I am assuming from your code that the title is the first line of the text file. If this is the case, it appears that you are missing two steps:

  1. You need to assign the value from the first read to the text box you want the title in.

  2. You then need to set the value of inputString to an empty string or use another variable to hold the read of the body text. This ensures you do not duplicate the title text in the body.

protected void FillForm(object sender, EventArgs e)
{

    string inputString;
    textBoxContents.Text = "";
    using (StreamReader streamReader = File.OpenText(@"E:\file.txt"))
    {
        inputString = streamReader.ReadLine();


        //assign inputString value to title text box
        //set inputString value to ""


        while (inputString != null)
        {
            textBoxContents.Text += inputString + "<br />";
            inputString = streamReader.ReadLine();
        }
    }
}

Hope it helps.

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 157136

You can try to find the first line and and split it there:

string text = File.ReadAllText(@"E:\file.txt");

int positionOfFirstLineEnd = text.IndexOf('\n');

string title = text.Substring(0, positionOfFirstLineEnd);
string description = text.Substring(positionOfFirstLineEnd + 1);

Upvotes: 0

Related Questions