locked
align text file line RRS feed

  • Question

  • i have a text file look like this 

      40611   9275   81504  -49757  
    42111   9275   106904  -49757  
      111111   51275   132304  -49757  
         49611   9275   157704  -49757  
      45111   -11725   183104  -49757  
     
    i have four column like this in my text file i want to align all this column so it will look like this

       40611   9275   81504  -49757  
       42111   9275  106904 -49757  
     111111  51275  132304 -49757  
       49611    9275  157704 -49757  
     i want to align so that all the number will be align to right

    how can i do this? 

    Tuesday, August 4, 2009 7:26 AM

Answers

  • You would need to read the file line-by-line and check the width of each number on the line and append the approproate number of spaces in front of the number. In the example code below I've set a fixed column width and a different outputfile to simplify the code. I've also assumed that the columns are separated by space(s).

            Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("c:\myinputfile.txt")
            Dim writer As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("c:\myoutputfile.txt", False)
    
            Dim line As String
            Dim lineParts As String()
            Dim outputLine As String
            Const columnWidth As Integer = 10
    
            'Read the first line
            line = reader.ReadLine
    
            'As long as there is more lines
            While Not line Is Nothing
    
                outputLine = ""
    
                'Split the line into parts using a single space as the separator
                'and ignore any empty elements
                lineParts = line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    
                'Iterate through the parts in the line
                For Each linePart As String In lineParts
    
                    'If the length of the part is less than the defined column width, add the
                    'correct number of spaces to make it right aligned
                    If linePart.Length < columnWidth Then
                        outputLine += Space(columnWidth - linePart.Length)
                    End If
                    outputLine += linePart
                Next
    
                writer.WriteLine(outputLine)
    
                'Read the next line from the file
                line = reader.ReadLine
            End While
    
            reader.Close()
            writer.Close()
    
    You could dynamically determine the column width by first going through the file to determine the widest number in each column and then using that information when formatting the output. If adding such a logic you could also read the file into a list in memory so that you only need to read the file once. You could also then write the formatted file back to the same file.
    • Proposed as answer by Jeff Shan Friday, August 7, 2009 2:07 AM
    • Marked as answer by Jeff Shan Friday, August 7, 2009 2:43 AM
    Tuesday, August 4, 2009 10:12 AM
  • You can also use String.Format to left or right align text.

    Here's a version that reads the file and writes it back into a new file formatted with String.Format.

            Dim all_text As String = My.Computer.FileSystem.ReadAllText("Values.txt")
            Dim lines() As String = all_text.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
            Dim result As String = ""
            For Each a_line As String In lines
                ' Get the values.
                Dim values() As String = a_line.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
                result &= String.Format("{0,10}{1,10}{2,10}{3,10}", _
                    values(0), values(1), values(2), values(3)) & vbCrLf
            Next a_line
            My.Computer.FileSystem.WriteAllText("FormattedValues.txt", result, False)
            Debug.WriteLine(result)

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    • Proposed as answer by Jeff Shan Friday, August 7, 2009 1:59 AM
    • Marked as answer by Jeff Shan Friday, August 7, 2009 2:43 AM
    Tuesday, August 4, 2009 12:41 PM

All replies

  • You would need to read the file line-by-line and check the width of each number on the line and append the approproate number of spaces in front of the number. In the example code below I've set a fixed column width and a different outputfile to simplify the code. I've also assumed that the columns are separated by space(s).

            Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("c:\myinputfile.txt")
            Dim writer As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("c:\myoutputfile.txt", False)
    
            Dim line As String
            Dim lineParts As String()
            Dim outputLine As String
            Const columnWidth As Integer = 10
    
            'Read the first line
            line = reader.ReadLine
    
            'As long as there is more lines
            While Not line Is Nothing
    
                outputLine = ""
    
                'Split the line into parts using a single space as the separator
                'and ignore any empty elements
                lineParts = line.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    
                'Iterate through the parts in the line
                For Each linePart As String In lineParts
    
                    'If the length of the part is less than the defined column width, add the
                    'correct number of spaces to make it right aligned
                    If linePart.Length < columnWidth Then
                        outputLine += Space(columnWidth - linePart.Length)
                    End If
                    outputLine += linePart
                Next
    
                writer.WriteLine(outputLine)
    
                'Read the next line from the file
                line = reader.ReadLine
            End While
    
            reader.Close()
            writer.Close()
    
    You could dynamically determine the column width by first going through the file to determine the widest number in each column and then using that information when formatting the output. If adding such a logic you could also read the file into a list in memory so that you only need to read the file once. You could also then write the formatted file back to the same file.
    • Proposed as answer by Jeff Shan Friday, August 7, 2009 2:07 AM
    • Marked as answer by Jeff Shan Friday, August 7, 2009 2:43 AM
    Tuesday, August 4, 2009 10:12 AM
  • You can also use String.Format to left or right align text.

    Here's a version that reads the file and writes it back into a new file formatted with String.Format.

            Dim all_text As String = My.Computer.FileSystem.ReadAllText("Values.txt")
            Dim lines() As String = all_text.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)
            Dim result As String = ""
            For Each a_line As String In lines
                ' Get the values.
                Dim values() As String = a_line.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
                result &= String.Format("{0,10}{1,10}{2,10}{3,10}", _
                    values(0), values(1), values(2), values(3)) & vbCrLf
            Next a_line
            My.Computer.FileSystem.WriteAllText("FormattedValues.txt", result, False)
            Debug.WriteLine(result)

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    • Proposed as answer by Jeff Shan Friday, August 7, 2009 1:59 AM
    • Marked as answer by Jeff Shan Friday, August 7, 2009 2:43 AM
    Tuesday, August 4, 2009 12:41 PM
  • Along with Rod's suggestion to use String.Format - I will point out that if you are not using a fixed width font in the textbox where this is displayed, your alignment will probably not be what you expect...



    Tom Shelton
    Tuesday, August 4, 2009 4:30 PM
  • You may also want to take a look at using the features available in StringBuilder:

    http://msmvps.com/blogs/deborahk/archive/2009/07/21/formatting-text-files.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, August 26, 2009 12:47 AM