Replace blank characters in a text file.
-
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.
-
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.
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 -
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, 2007I'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.
-
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.
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 WhileA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
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.
-
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 WhileA guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
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