I'm trying to use the Streamreader function to pull text from a text file and make it into a string, and then place it in a list, and this works just fine for at the start of the line, but I can't figure out how to tell it to extract from a specific keyword, say "key" and end at a certain keyword, say "word" into a string. Can anyone give me a hand?
How to use Streamreader to read to and from a specific point?
Collapse
X
-
Tags: None
-
I think you may have misunderstood me, I mean how do I do this if I have a line of text like "This is a line of text containing a keyword right in the middle.", how would I get Streamreader to extract the keyword?Comment
-
I am not too familiar with StreamReader but if you can access the BaseStream, you should be able to use BaseStream.seek ...
Try this:
Code:public static string GetPartOfFile() { using (var myReader = new StreamReader(@"c:\myFile.txt")) { myReader.BaseStream.Seek(100, SeekOrigin.Begin); string output = myReader.ReadToEnd(); return output; } }
Comment
-
Perhaps this will help.
What I'm working on is a program that reads text files, pulls out any URL's that are in it, and opens it up in the user's default browser if possible, if not, displays them in a nice list. The part that I am working on specifically, is pulling things out from midway in a line. So for example if the line went "Visit us at www.example.com please!" Then all I would want from that is "www.example.co m". If this were to be at the start of a line, it would be quite simple, but I honestly have no clue how to do this if it is in the middle of a line. I figure I need to somehow work with a delimiter, such as a space in this case, then trim the space off. I just don't know how to go about doing that.
The part that I'm working on:
Code:else if(line.Contains("www") == true) { //This is where the read code would go.. count++; lineList.Add(midLine); }
This is the entire method, if it helps in any manner.
Hopefully it is explanatory enough..
Code:private void GetLines() { count = 0; //Will run as long as the file is real. if (links != null) { string line; StreamReader lines = new StreamReader(links); while ((line = lines.ReadLine()) != null) { //Makes sure the line isn't blank if (line.Trim().Length != 0) { //Checks to see if the line starts with http if (line.StartsWith("http") == true) { count++; //Adds to the count and adds to the list lineList.Add(line); } else if(line.Contains("www") == true) { //This is where I need help.. count++; lineList.Add(midLine); } //Checks to see if the line starts with www if (line.StartsWith("www") == true) { count++; //Adds to the count and adds to the list lineList.Add(line); } } } }
Comment
-
use a regular expression for URL format on the the line. If the line is a match but it in your string list or array.
Code:List<string> list = new List<string>(); using(TextReader reader = new StreamReader(/*filename*/) { while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); if (Regex.IsMatch(line, your url pattern, Regex.IgnoreCase)) list.Add(line);[CODE]
}[/CODE]
Another way is to use
Code:List<string>list = new List<string>(); string text = null; using(TextReader reader = new StreamReader(/*filename*/) { text = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(text) { MatchCollection matches = Regex.Matches(text,your url pattern, Regex.IgnoreCase); if (matches.Count > 0) { foreach(Match m in matches) { list.Add(text.Substring(m.Index,m.Length)); } } }
Comment
Comment