Peter Duniho said:
It all depends on what you need at the end.
If you actually want an array or list of strings, one entry per line, then
using StreamReader (as in your initial example) and adding each line to
your data structure (I'd start with a List<string>, use ToArray() later if
you want an array) would make the most sense.
If you simply want to do something special each time you find a line with
some specific characteristic, then StreamReader is still likely to fit
best, but of course you don't need to save the lines as you read them.
If you really do want a single string representing the whole file _and_
you want to detect specific kinds of text, then perhaps the best solution
is to use ReadAllText(), and then use the System.Text.Regex class to
process the string, looking for whatever specific patterns you want.
Pete
Regex is probably exactly what i need, but i've read about it so many times
and been completely unable to grasp the inscrutable code to grab what i'm
looking for.
I'm parsing lisp code text files.
one objective is to strip out all the comments from the actual code
leading comments are easy to get rid of
if a line begins with the comment character (
![Wink ;) ;)](/styles/default/custom/smilies/wink.gif)
ignore the whole line
if it's at the end of a line i loop through the characters in the line
looking for ;
however, if that ; occurs inside a 'quote' (text enclosed in ""), it's not a
comment character, it's a literal
so i have to know if i'm inside a 'quote' (which can span multiple lines)
"this is a ; in a quote "
"this is
also
a ; in
a quote "
i'm using the term quote to indicate characters in between " and " (which is
a "string" data type in lisp terminology)
i have fairly complicated code that does all this with about 99% accuracy
but still a few odd 'strings' (characters between double quotes) will throw
it off, generally because a 'string' starts with a double quote and end with
a double quote, so i track open and close quotes to know if i'm inside a
string...however it can also contain a quote inside the string if it's
escaped with a \.
"this is a string with a \" inside it"
i can probably muck around some more with my code to fix that problem
but i know the way i'm doing it is a travesty of ugliness
i'm not sure regex can do all that but i suspect it could if i grasped all
the nuances of how to construct the regex string
mark
sorry for the long winded explanation but i don't know a simpler way to
describe what i'm trying to do
i can show the vb6 code that does this if desired, but doubt anyone wants to
see that ugliness
![Smile :-) :-)](/styles/default/custom/smilies/smile.gif)