我正在阅读一个主要是字母字符的文本文件。内容并不真正相关,但每行的大小非常重要。我将提供此文本的过程将要求每行不超过50个字符。所以我会预先处理文本并添加换行符以确保发生这种情况。
我尝试了几个类似^。* $的VB.NET正则表达式,但这并没有真正将这些行分解为50个字符。我将获取结果并遍历每个匹配,然后将其剪切并将其写入内存中的对象。这可以通过一次正则表达式传递吗?
否则我会使用一个streamreader并在每一行检查长度,如果< = 50用一个streamwriter写出来。如果> 50将它切成50个部分,然后使用streamwriter。
我的文字的简短例子:
119 SMITH KATY AAAA F ZZZ X NB SX ET
MILES,200/LM450
120 JONES THOMAS W QQQ 66-W NB OS SC LW EP
ET
L/G/B/MAY20-2010/JONES/THOMAS/KEITH 121 BUBBA BILLY HH4 S XQT 2PA-F 1 IP SC LH ET
DOCC
122 NEWTON IAASAC S FTY 240-U NB QC LF KD EE
正在寻找有关如何有效执行此操作的提示。
更新:我最终使用了SSS建议的streamreader方法。但是,我试图避免使用旧的Mid函数并坚持使用Substring。因此,我不得不做一些检查,并使用另一个SO帖子的一些代码,但不能记住哪一个。无论如何它是:
Dim reader As New StringReader(aSource)
Dim line As String = Nothing
Dim writer As New StringWriter
Dim chunkSize As Integer = 50
Dim chunk As String
Do
line = reader.ReadLine()
If Not String.IsNullOrEmpty(line) Then
Debug.WriteLine(line.Length & "-->" & line)
'if line length is less than or equal to chunk size then write it out, otherwise cut it up and then write the chunks out
If line.Length <= chunkSize Then
writer.WriteLine(line)
Else
Debug.WriteLine("---------------------")
For i = 0 To line.Length Step chunkSize
Debug.WriteLine("i =" & i)
Debug.WriteLine("i+c=" & i + chunkSize)
Debug.WriteLine("L =" & line.Length)
If i + chunkSize > line.Length Then
chunk = line.Substring(i, line.Length - i)
Else
chunk = line.Substring(i, chunkSize)
End If
Debug.WriteLine(" " & chunk.Length & "-->" & chunk)
writer.WriteLine(chunk)
Next i
Debug.WriteLine("---------------------")
End If
End If
Loop While (line IsNot Nothing)
reader.Close()
reader.Dispose()
'this cut string now becomes our source
Debug.WriteLine("==>" & writer.ToString)
sourceText = writer.ToString
writer.Close()
writer.Dispose()
希望帮助有同样问题的人。
答案 0 :(得分:0)
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim strFilenameIn As String = "C:\Junk\Input.TXT"
Dim strFilenameOut As String = "C:\Junk\Output.TXT"
Try
Using sr As New StreamReader(strFilenameIn)
Using sw As New StreamWriter(strFilenameOut)
Do
Dim strLine As String = sr.ReadLine()
If strLine Is Nothing Then Exit Do 'EOF
For i As Integer = 1 To strLine.Length Step 50
sw.WriteLine(Mid(strLine, i, 50))
Next i
Loop
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
MsgBox(strfilenameout & " written")
End Sub
End Class