[What I have is this files which contain some strings and separated with colon.][1]
[This is the output what I want, there are some spaces and without colon between two strings.][2]
[The question is, my output is this picture, the first row is fine, but from the second row, the first and second strings are not good, I want them to be like first row][3]
Dim splitFile = File.ReadAllText(Result).Split(",")
For count = 0 To splitFile.Length - 1
While (splitFile(count).Length < 20)
splitFile(count) = splitFile(count) " "
End While
totalFile = splitFile(count)
Next
My.Computer.FileSystem.WriteAllText("C:test.txt", totalFile, False)
strong text [1]: https://i.stack.imgur.com/fSMjE.png [2]: https://i.stack.imgur.com/H4LPH.png [3]: https://i.stack.imgur.com/LWwyF.png
CodePudding user response:
This should do as you want:
''' <summary>
''' Removes delimiters from a file and makes all but the last column a fixed width.
''' </summary>
''' <param name="inputFilePath">
''' The path of the input file.
''' </param>
''' <param name="outputFilePath">
''' The path of the output file. Can be the same as the input file.
''' </param>
''' <param name="delimiter">
''' The field delimiter in the input file.
''' </param>
''' <param name="fieldWidth">
''' The width of the columns in the output file.
''' </param>
Private Sub MakeDelimitedFileFixedWidth(inputFilePath As String,
outputFilePath As String,
delimiter As String,
fieldWidth As Integer)
'The lines to write to the output file.
Dim outputLines As New List(Of String)
For Each line In File.ReadLines(inputFilePath)
'Split the existing line on the delimiter.
Dim fields = line.Split({delimiter}, StringSplitOptions.None)
'Pad all but the last column to the specified width.
For i = 0 To fields.Length - 2
fields(i) = fields(i).PadRight(fieldWidth)
Next
outputLines.Add(String.Concat(fields))
Next
'Write out the processed data.
File.WriteAllLines(outputFilePath, outputLines)
End Sub
That will not pad the last column. If you want that then change fields.Length - 2
to fields.Length - 1
or fields.GetUpperBound(0)
.
In your case, you would call it like so:
MakeDelimitedFileFixedWidth(Result, "C:test.txt", ",", 20)
While I haven't examined your existing code in detail, the issue is probably that you were reading the existing data as a single block, then splitting that on the delimiters. That would mean that you would have the last field from one line and the first field from the next line together as one value. That would play havoc with your field length calculations. The code I have provided reads the existing data line by line, so that issue doesn't exist.
CodePudding user response:
I think that's because you are reading all the lines as a whole. The end of each line should be a \n
symbol, thus the last string of the first line cannot be separated with the first string of the second line. The concatenated string is longer than the length you have defined (i.e. 20), therefore no space is added. You should read the file line by line and process each line separately.
BTW, to add tracing spaces to get fixed length strings, you may want to try String.Format which performs much better.
Check ref here. Example:
String.Format({0:-20}, splitFile(count))