Search and Replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RickN

    #1

    Search and Replace

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

    Thanks,
    RickN


  • Greg Ewing [MVP]

    #2
    Re: Search and Replace

    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]
    IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.


    "RickN" <rick@abasoftwa re.com> wrote in message
    news:eKLU9vGhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=blue]
    > What is the best way to open a file
    > and perform a search and replace for multiple char values.
    >
    > Thanks,
    > RickN
    >
    >[/color]


    Comment

    • RickN

      #3
      Re: Search and Replace

      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(newlin e) 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_SPA M_gewing.com> wrote in message
      news:e3gwGALhDH A.2512@TK2MSFTN GP09.phx.gbl...[color=blue]
      > 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[/color]
      keep[color=blue]
      > track of what bytes you have already seen and whether the previous bytes[/color]
      and[color=blue]
      > the newest bytes are a match. All in all I would go with one of the[/color]
      Replace[color=blue]
      > functions.
      >
      > --
      > Greg Ewing [MVP]
      > http://www.citidc.com
      >
      > "RickN" <rick@abasoftwa re.com> wrote in message
      > news:eKLU9vGhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=green]
      > > What is the best way to open a file
      > > and perform a search and replace for multiple char values.
      > >
      > > Thanks,
      > > RickN
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Greg Ewing [MVP]

        #4
        Re: Search and Replace

        RickN, yea, with an unknown file size, that's the best way to do it.

        --
        Greg Ewing [MVP]
        IT support built exclusively for nonprofits. 25 years of managed IT, help desk, and cybersecurity - so your team can focus on your mission.


        "RickN" <rick@abasoftwa re.com> wrote in message
        news:#B#xkKQhDH A.4468@TK2MSFTN GP12.phx.gbl...[color=blue]
        > 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(newlin e) 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_SPA M_gewing.com> wrote in message
        > news:e3gwGALhDH A.2512@TK2MSFTN GP09.phx.gbl...[color=green]
        > > 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[/color][/color]
        you[color=blue][color=green]
        > > want to do it. If your file is too big you could read the file in byte[/color][/color]
        by[color=blue][color=green]
        > > byte and match on the bytes. That'll be a bit harder since you have to[/color]
        > keep[color=green]
        > > track of what bytes you have already seen and whether the previous bytes[/color]
        > and[color=green]
        > > the newest bytes are a match. All in all I would go with one of the[/color]
        > Replace[color=green]
        > > functions.
        > >
        > > --
        > > Greg Ewing [MVP]
        > > http://www.citidc.com
        > >
        > > "RickN" <rick@abasoftwa re.com> wrote in message
        > > news:eKLU9vGhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > What is the best way to open a file
        > > > and perform a search and replace for multiple char values.
        > > >
        > > > Thanks,
        > > > RickN
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...