Results 1 to 7 of 7

Thread: Removing empty lines in a text file?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210

    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]

  2. #2
    New Member
    Join Date
    Mar 2004
    Posts
    6
    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.

  3. #3
    New Member
    Join Date
    Mar 2004
    Posts
    6
    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

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    well, assuming you are reading it from a file, then this should work:

    VB Code:
    1. ' Imports System.IO
    2.  Private Sub removeEmptyLines(ByVal filePath As String)
    3.         ' Open the file for reading
    4.         Dim sr As New StreamReader(filePath)
    5.         ' Read all the data in the file and store it in the content variable
    6.         Dim content As String = sr.ReadToEnd
    7.         sr.Close()
    8.  
    9.  
    10.         ' Open the file for writing
    11.         Dim sw As New StreamWriter(filePath)
    12.         ' Break up the data into array of lines
    13.         Dim lines() As String = content.Split(Environment.NewLine)
    14.         Dim aLine As String
    15.  
    16.         ' Examine each line
    17.         For Each aLine In lines
    18.             ' Only write back the line if its not empty
    19.             If aLine.Trim() <> "" Then
    20.                 sw.WriteLine(aLine)
    21.             End If
    22.         Next
    23.  
    24.         sw.Flush()
    25.         sw.Close()
    26.     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!!

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    They recommend loading any text file that's more than 10k into stringbuilder .It's more efficient .

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    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]

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
  •  



Click Here to Expand Forum to Full Width