473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Search and Replace

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

Thanks,
RickN
Nov 15 '05 #1
3 2407
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.

--
Greg Ewing [MVP]
http://www.citidc.com

"RickN" <ri**@abasoftware.com> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
What is the best way to open a file
and perform a search and replace for multiple char values.

Thanks,
RickN

Nov 15 '05 #2
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

"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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.

--
Greg Ewing [MVP]
http://www.citidc.com

"RickN" <ri**@abasoftware.com> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
What is the best way to open a file
and perform a search and replace for multiple char values.

Thanks,
RickN


Nov 15 '05 #3
RickN, yea, with an unknown file size, that's the best way to do it.

--
Greg Ewing [MVP]
http://www.citidc.com

"RickN" <ri**@abasoftware.com> wrote in message
news:#B**************@TK2MSFTNGP12.phx.gbl...
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

"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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.

--
Greg Ewing [MVP]
http://www.citidc.com

"RickN" <ri**@abasoftware.com> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
What is the best way to open a file
and perform a search and replace for multiple char values.

Thanks,
RickN



Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8696
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
10
3099
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
22
11076
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
5
2610
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
32
14753
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
5065
by: Dennis | last post by:
I am trying to implement a "Find and Replace" dialog that allows using wildcards in the find string, much like the Find and Replace Dialogs in Ms Word, etc. Are there any references or examples on...
2
5047
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
9
2228
by: tomjones75 | last post by:
dear community, i want to search the content of all fields in one table in a access database. it already works for the content of one field in the table. please take a look at the code in...
14
2237
by: Simon Gare | last post by:
Hi, have a search.asp page with results.asp page drawing data from an SQL db, problem is the user has to type the whole field value into the search box to retrieve the value on results.asp, what...
1
7508
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.