|
-
Mar 13th, 2004, 09:29 PM
#1
Thread Starter
Addicted Member
Removing empty lines in a text file?
hi.
i'm working with a huge (HUGE) list. and i'm trying to remove every empty lines manually, but i thought to myself, "hey, idiot, that's what programming is for." i agree with him.
so, without removing spaces between words, how can i remove empty lines? i suppose there's a way to search every line of a file and remove the line where it's ""
[vbcode]
Me.Hide()
[/vbcode]
-
Mar 13th, 2004, 11:35 PM
#2
New Member
Off the top of my head, I think if you use the split function, and split it from the new line character (vbcrlf?), and then go through the array and if there are any not any characters other then a space, then delete that out of the array, and when done, rebuild it adding the vbcrlf back in at the end of each line. It would be pretty cpu intensive I think, having to split it, test each line, and then rebuild it, so there has got to be an easier way, but I can't think of another way off hand.
-
Mar 13th, 2004, 11:52 PM
#3
New Member
Wow, it worked. I was curious if what I said would allow it to work, so I wrote up the code real quick to see. I had to make some slight adjustments, but compiled and worked correctly first time I tried it.
Here is the code: (my work computer is temporarily without internet, so I am just typing this in manually instead of copying/paste, so hopefully won't be any typos.
dim teststring as string
teststring = teststring + "test1" & vbcrlf
teststring = teststring + "" & vbcrlf
teststring = teststring + "test2" & vbcrlf
msgbox(teststring)
msgbox(test(teststring))
private function test(teststring as string) as string
dim temparray as array = split(teststring, vbcrlf)
for I as integer = 0 to temparray.length - 1
dim temparray2 as array = temparray(I).tochararray
For R as integer = 0 to temparray2.length - 1
if temparray2(R) <> " " or temparray2(R) <> vbcrlf then
temparray(I) = temparray(I) + vbcrlf
exit for
else
temparray(I) = ""
end if
next
next
dim newstring as string
for I as integer = 0 to temparray.length - 1
newstring = newstring + temparray(I)
next
return newstring
end function
-
Mar 14th, 2004, 02:36 AM
#4
well, assuming you are reading it from a file, then this should work:
VB Code:
' Imports System.IO
Private Sub removeEmptyLines(ByVal filePath As String)
' Open the file for reading
Dim sr As New StreamReader(filePath)
' Read all the data in the file and store it in the content variable
Dim content As String = sr.ReadToEnd
sr.Close()
' Open the file for writing
Dim sw As New StreamWriter(filePath)
' Break up the data into array of lines
Dim lines() As String = content.Split(Environment.NewLine)
Dim aLine As String
' Examine each line
For Each aLine In lines
' Only write back the line if its not empty
If aLine.Trim() <> "" Then
sw.WriteLine(aLine)
End If
Next
sw.Flush()
sw.Close()
End Sub
I'm not really sure, but I would say it may be a bit inefficient to load the whole file in a string and then process it... there may be a better way around but if your file isnt too big I wouldnt worry about it
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Mar 14th, 2004, 05:31 AM
#5
Sleep mode
They recommend loading any text file that's more than 10k into stringbuilder .It's more efficient .
-
Mar 14th, 2004, 02:05 PM
#6
Thread Starter
Addicted Member
thanks guys. i'll try it.
but i shouldn't program and write an essay that's due in 5 days...
[vbcode]
Me.Hide()
[/vbcode]
-
Mar 14th, 2004, 02:26 PM
#7
Originally posted by Pirate
They recommend loading any text file that's more than 10k into stringbuilder .It's more efficient .
hmm lets say you load the file into the string builder, then again you'll have to convert it to string if you want to split it into lines... or otherwise you should read the file line by line? 
nahya^^ essays are evil yeah work on that
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|