Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Replace blank characters in a text file.

Replace blank characters in a text file.

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Diego F
    wrote on last edited by
    #1

    Hello. That must be quite simple. I have a text file and I want to replace spaces with returns. I'm trying that code, but it doesn't work: Dim i As Integer = 1 While Not myreader.EndOfStream line = myreader.ReadLine() line = i & line.Replace(" "c, vbCrLf) mywriter.WriteLine(line) i += 1 End While myreader is a StreamReader and mywriter is a StreamWriter. Line is a String. Do you see the problem here?

    Regards, Diego F.

    D D 2 Replies Last reply
    0
    • D Diego F

      Hello. That must be quite simple. I have a text file and I want to replace spaces with returns. I'm trying that code, but it doesn't work: Dim i As Integer = 1 While Not myreader.EndOfStream line = myreader.ReadLine() line = i & line.Replace(" "c, vbCrLf) mywriter.WriteLine(line) i += 1 End While myreader is a StreamReader and mywriter is a StreamWriter. Line is a String. Do you see the problem here?

      Regards, Diego F.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Standard Question #1: What does "it doesn't work" mean? What happens? Do you get an error message? What does the expected output look like? What does the actual output look like? Are both streams created on the same file? You can NOT do that. You can not replace a line in a text file. You have to write an entirely new text file with the modified data.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Standard Question #1: What does "it doesn't work" mean? What happens? Do you get an error message? What does the expected output look like? What does the actual output look like? Are both streams created on the same file? You can NOT do that. You can not replace a line in a text file. You have to write an entirely new text file with the modified data.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        D Offline
        D Offline
        Diego F
        wrote on last edited by
        #3

        I'm sorry for the loose explanation. myreader references the original file and mywriter a new file The original file contains large lines with some spaces. I want to insert line breaks to see the file with more ease. With the code above, the resulting file is just as the original file. No line breaks are inserted.

        Regards, Diego F.

        D 1 Reply Last reply
        0
        • D Diego F

          I'm sorry for the loose explanation. myreader references the original file and mywriter a new file The original file contains large lines with some spaces. I want to insert line breaks to see the file with more ease. With the code above, the resulting file is just as the original file. No line breaks are inserted.

          Regards, Diego F.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          OK. Are you sure that the whitespace characters in the original file are spaces and not some other non-visibile character, like Tab? The code, as you have it, should work, but only for single spaces. You'd be better off using a Regular Expression that can replace all kinds of whitespace characters without you knowing what they are ahead of time:

          Imports System.Text.RegularExpressions
          .
          .
          .
          ' Define a RegEx expression that matches strings
          ' of 1 or more whitespace characters.
          Dim re As New Regex("\s+")
          Dim i As Integer = 1
          While Not myreader.EndOfStream
          line = myreader.ReadLine()
          line = i & re.Replace(line, vbCrLf)
          mywriter.WriteLine(line)
          i += 1
          End While

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          D 1 Reply Last reply
          0
          • D Diego F

            Hello. That must be quite simple. I have a text file and I want to replace spaces with returns. I'm trying that code, but it doesn't work: Dim i As Integer = 1 While Not myreader.EndOfStream line = myreader.ReadLine() line = i & line.Replace(" "c, vbCrLf) mywriter.WriteLine(line) i += 1 End While myreader is a StreamReader and mywriter is a StreamWriter. Line is a String. Do you see the problem here?

            Regards, Diego F.

            D Offline
            D Offline
            Diego F
            wrote on last edited by
            #5

            I replaced " "c for Chr(0) and now it works.

            Regards, Diego F.

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              OK. Are you sure that the whitespace characters in the original file are spaces and not some other non-visibile character, like Tab? The code, as you have it, should work, but only for single spaces. You'd be better off using a Regular Expression that can replace all kinds of whitespace characters without you knowing what they are ahead of time:

              Imports System.Text.RegularExpressions
              .
              .
              .
              ' Define a RegEx expression that matches strings
              ' of 1 or more whitespace characters.
              Dim re As New Regex("\s+")
              Dim i As Integer = 1
              While Not myreader.EndOfStream
              line = myreader.ReadLine()
              line = i & re.Replace(line, vbCrLf)
              mywriter.WriteLine(line)
              i += 1
              End While

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              D Offline
              D Offline
              Diego F
              wrote on last edited by
              #6

              Thank you for your help.

              Regards, Diego F.

              1 Reply Last reply
              0
              • D Diego F

                I replaced " "c for Chr(0) and now it works.

                Regards, Diego F.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                This would answer the question in my previous reply. Those spaces are NOT spaces. The space character you specified is ASCII character 32. The character you supplied in this post is ASCII character 0.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups