Home > front end >  How can I read a text file then only display the text that is between line breaks
How can I read a text file then only display the text that is between line breaks

Time:10-08

I am trying to read a text file but then only display the text that is between two line breaks. So for example if I have this

 <== Line Break ==> (1)
 This is my text
 <== Line Break ==> (2)
 Some Text
 Some Text
 Some Text
 Some Text
 <== Line Break ==> (3)
 Result
 <== Line Break ==> (4)
 Some other text not needing to show

So as you can see above I want to display the text between "Line Break" 1 and 4 only What I have at the moment is this:

 string file = "My Text File.txt";
        string[] str = null;
        if (File.Exists(Server.MapPath(file)))
        {
            str = File.ReadAllLines(Server.MapPath(file));
        }
        foreach (string s in str)
        {
            if (s.Contains("This is my text"))
            {
                btnText.Text = s.Replace(s, "Heading 1");
                btnText.OnClientClick = ReadAdvertisingTest();
            }

            else if (s.Contains("This is my failed text"))
            {
                lblError.Text = lblError.Text   "\n"   s.Replace(s, "Heading 2");
            }
        }

 private string ReadAdvertisingTest()
  {
    <== This is where I need to display the text I want to have
  }

The above code works great by finding the text then displaying in a Button but I am not sure how to get the second part to work.

EDIT So the output should then be:

This is my text
 <== Line Break ==> (2) 
 Some Text
 Some Text
 Some Text
 Some Text
 <== Line Break ==> (3)
 Result

So basically keep Line break 2 and 3 (if possible)

Any suggestions would help

Thanks

CodePudding user response:

    StreamReader sr = new StreamReader(sfile);
    
    while (sr.Peek() != -1)
        {
          
    string strwithoutSplitData = sr.ReadLine().Trim();
      if(strwithoutSplitData.Contains("<Header>") || strwithoutSplitData.Contains("<Footer>"))
    {
    
    if (strwithoutSplitData.Contains("<Footer>"))
    {
    sr.close();
    break;
    }
    }
    else
    {
    //do something
    }
 }

CodePudding user response:

Ok, this is a bit ugly, but it will work:

string fPath = @"c:\Test5\mytext.txt";
string strBuf = File.ReadAllText(fPath);
TextBox1.Text = strBuf;

List<string> MyList = strBuf.Split(new[] { System.Environment.NewLine }, StringSplitOptions.None).ToList();

MyList.RemoveAt(0);
if (MyList.Count > 2)
   MyList.RemoveRange(3, MyList.Count - 3);
 
string strResult = string.Join(System.Environment.NewLine,MyList);
TextBox2.Text = strResult;

So I read the text file, put it in a text box, and then 2nd text box shows the results:

enter image description here

In fact, this suggests quite much:

skip first line
grab all text to next line
include the next line

So, in place of that split() function, we could probably just concentate the result of the split and grab the 3 values in place of a "join", but above should suffice anyway.

  • Related