0

我的数据如下所示:

8/29/2013<tab>1<tab>name<tab>aaaaaaaaaaa<tab>12
8/29/2013<tab>22<tab>asd<tab>asd<tab>123
8/29/2013<tab>23<tab>xycabc<tab>asd<tab>12

我需要这样的东西:

RECEIVED on:8/29/2013 FROM:name AMOUNT:12
RECEIVED on:8/29/2013 FROM:asdAMOUNT:123

我试过这个:

Dim rvsr As New IO.StreamReader(vcFile)
Dim vText As String
Dim vstring(-1) As String
p1 = "    "
Dim vData As String = ""
While rvsr.Peek <> -1
    vText = rvsr.ReadLine()
    vstring = vText.Split(p1)
    vData = vData + vbCrLf + "RECEIVED ON: " + vstring(0) + " FROM: " + vstring(1) + " AMOUNT: " + vstring(2)
End While
RichTextBox_WD.Text = vData
rvsr.Close()
4

2 回答 2

2

如果不向我们展示你得到了什么,我无法确定到底发生了什么。但是在查看您的代码时,我认为问题与您使用拆分的方式有关。vbTab我已经使用常量和使用字符串分隔符的方法修改了您的代码以适合控制台应用程序String.Split,看看这是否是您想要的。

Imports System.IO

Module Module1

    Sub Main()

        Dim vText As String
        Dim vstring(-1) As String
        Dim p1 As String() = {vbTab} 'Note I am using a string array and the vbTab Constant
        Dim vData As String = ""
        Using rvsr As New StreamReader("C:\\temp\\source.txt")
            While rvsr.Peek <> -1
                vText = rvsr.ReadLine()
                vstring = vText.Split(p1, StringSplitOptions.RemoveEmptyEntries) 'I am also using the option to remove empty entries a
                vData = vData + vbCrLf + "Recieved On:" + vstring(0) + " From:" + vstring(2) + " Amount:" + vstring(4)

            End While
        End Using
        Console.Write(vData)
        Console.ReadLine()
    End Sub

End Module
于 2013-08-30T04:57:13.550 回答
0
RichTextBox_WD.Text = String.Join(vbCrLf, File.ReadLines(vcFile).Select(Function(line)
                                                                            Dim format = "RECEIVED ON: {0} FROM: {1} AMOUNT {2}"
                                                                            Dim fields = line.Split(vbTab)
                                                                            Return String.Format(format, fields(0), fields(1), fields(2))
                                                                        End Function)

我更喜欢创建一个包装的Joined扩展方法(采用分隔符和一个可选的转换委托),以及一个包装的扩展方法:IEnumerable<T>String.JoinFormattedStringString.Format

RichTextBox_WD.Text = File.ReadLines(vcFile).Joined(vbCrLf, Function(line)
                                                                Dim format = "RECEIVED ON: {0} FROM: {1} AMOUNT {2}"
                                                                Dim fields = lines.Split(VbTab)
                                                                Return format.Formmatted(fields(0), fields(1), fields(2))
                                                            End Function)
于 2013-08-30T04:23:09.100 回答