Search and Replace

  • Thread starter Thread starter RickN
  • Start date Start date
R

RickN

What is the best way to open a file
and perform a search and replace for multiple char values.

Thanks,
RickN
 
RickN, once you have the file in a string, assuming it's not to big for
that, I would use string.Replace() or Regex.Replace() depending on how you
want to do it. If your file is too big you could read the file in byte by
byte and match on the bytes. That'll be a bit harder since you have to keep
track of what bytes you have already seen and whether the previous bytes and
the newest bytes are a match. All in all I would go with one of the Replace
functions.
 
Thanks,
I probably wasn't real clear on the question so let me restate it.
I have an existing file and I have several char's that I want
to replace with different values.
What I've done is:
created a FileStream to open the existing file
created a FileStream to creat a new file
created a StreamReader sr and a StreamWriter sw
looped through the file reading in each line with the sr
performing a string.replace(old,new) separately for each
pair of replacement values.
sw.Write(newline) to the new file
deleted the old file
I realize I could probably read in the whole file into memory but the file
could be large. Other then that, is this
the best way to handle this problem.
Thanks,
RickN
 
Back
Top