逐行读取制表符分隔的文本文件获取 vb.net 中 <tab> 之后的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18524530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:52:00  来源:igfitidea点击:

reading a tab delimited text file line by line get data after <tab> in vb.net

vb.netvb.net-2010

提问by Dilip

My data looks like this:

我的数据如下所示:

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

and i need something like this:

我需要这样的东西:

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

I've tried this:

我试过这个:

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()

回答by Mark Hall

Without showing us what you are getting I can not be sure what exactly is happening. But in looking at your code I believe the problem has to do with how you are using your Split. I have modified your code to fit in a console application using the vbTabConstant and the String.SplitMethod that uses string seperators, see if this is what you are wanting.

如果没有向我们展示您得到了什么,我无法确定到底发生了什么。但是在查看您的代码时,我认为问题与您使用 Split 的方式有关。我已经使用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

回答by Zev Spitz

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)

I prefer to create a Joinedextension method on IEnumerable<T>which wraps String.Join(taking a delimiter and an optional transformation delegate), and a Formattedextension method on Stringwhich wraps String.Format:

我希望创建一个Joined扩展方法上IEnumerable<T>它包装String.Join(以分隔符和一个可选的转变代表),以及Formatted对扩展方法,String它包装String.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)