好吧,我需要从.txt中读取列和行 并获得我需要的数据。
示例:文件" sample.txt"有以下内容
C =列
R =行
C1 C2 C3
R1 0 0 0 3694440008091082330089 ALBINA HANSEN OF OLMEDO 00000075000000022091401520117873
我需要为每一行提供以下数据:
000369444000809108 2330089 ALBINA HANSEN DE OLMEDO 000000 75000 0000 220914 0 1 520117873
(第一行价值之上的一系列数字和字母)
第一个价值:从C19到C25(2330089)开始
第二价值:从C28到C50(ALBINA HANSEN DE OLMEDO)开始
第三个值:从C64到C68(75000)
第四项价值:从C73至C78(220914)开始
第五个值:它只是C80列(1)
我需要在消息框中显示数据:
我有以下代码:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\cogentvaio\Desktop\Banco Itau\TXT-FOMENTO ANGELIUS.txt")
MsgBox(fileReader)
End Sub
End Class
这允许我阅读.txt
的全部内容希望你能帮我解决这个问题,我正在学习在vb.net中编程。
我将向您展示我的代码,我无法看到使用substrinng创建的边界,我使用了您建议的代码,
进口系统 进口System.IO 公共类Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\cogentvaio\Desktop\Banco Itau\ejemplo\-TXT-ITAU ANGELIUS.txt")
Dim line As String
For Each line In fileReader
If line.Length > 80 Then
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(28, 50)
Dim c3 = line.Substring(64, 68)
Dim c4 = line.Substring(73, 78)
Dim c5 = line.Substring(80)
End If
Next
MessageBox.Show(fileReader)
End Sub
结束班
我得到的是这个
我不知道我的错误在哪里
答案 0 :(得分:0)
string.Substring是你的朋友。此方法从char索引开始从字符串中提取子字符串,并返回所请求的字符数(长度)。还要记住,数组(字符串是字符数组)始终在索引0处开始,因此您的值Cx值应该减1。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Instead of ReadAllText, use ReadAllLines to have your input splitted in an array of strings
Dim fileReader = System.IO.File.ReadAllLines(......)
for each line in fileReader
' To avoid nasty exceptions be sure that the line is at least 81 chars long
if line.Length > 80 Then
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(27, 23)
Dim c3 = line.Substring(63, 5)
Dim c4 = line.Substring(72, 6)
Dim c5 = line.Substring(79, 1)
.... process these values....
.... for example ....
Dim newLine = string.Join(" ", c1,c2,c3,c4,c5)
MessageBox.Show(newLine)
End if
Next
End Sub
此更改需要一行,提取零件并创建一个新字符串,其中只需要与空间连接在一起的所需部件。然后打印出这个新文本。当然,您需要将所有行与这些子部分连接起来,然后将newLine变量添加到List( Of String)
,然后执行单行的最终连接
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Instead of ReadAllText, use ReadAllLines to have your input splitted in an array of strings
Dim fileReader = System.IO.File.ReadAllLines(......)
Dim subText = New StringBuilder()
for each line in fileReader
if line.Length > 80 Then
....
Dim newLine = string.Join(" ", c1,c2,c3,c4,c5)
subText.Add(newLine)
End if
Next
MessageBox.Show(string.Join(Environment.NewLine, subText))
但是,您的索引似乎没有正确指向所需的数据。 这个子字符串提取似乎可以正确找到您的数据
Dim c1 = line.Substring(18, 7)
Dim c2 = line.Substring(26, 23)
Dim c3 = line.Substring(56, 5)
Dim c4 = line.Substring(65, 6)
Dim c5 = line.Substring(72, 1)