I have a text file with content as:
**************
Some text
**************
I want to read the text between **** in c#. How can I achieve the same in c sharp
I have a text file with content as:
**************
Some text
**************
I want to read the text between **** in c#. How can I achieve the same in c sharp
You could use ReadAllText
to get the contents of the file, then Replace
and Trim
to remove the unwanted contents of the file:
var result = System.IO.File.ReadAllText(@"c:\path\to\file.txt")
.Replace("*", string.Empty) // remove the asterisks
.Trim(); // trim whitespace and newlines
Here's how to read lines from a text file in general : What's the fastest way to read a text file line-by-line?
You can do this:
var lines = File.ReadAllLines(fileName);
int numLines = lines.Length;
for (int lineCounter = 1 /*discard the first*/;lineCounter<Length-1 /*discard the last*/;lineCounter++)
{
Do whatever you want with lines[lineCounter]
}
If you know the **** count then this might help.
string readContents;
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
{
readContents = streamReader.ReadToEnd();
int start = readContents.IndexOf("*****") + 1;
int end = readContents.LastIndexOf("*****", start);
string result = readContents.Substring(start, end - start);
}