VB.NET String预先拆分(C#或VB.NET)

时间:2013-10-30 01:45:43

标签: c# .net regex vb.net

这是我要分割的文本行,data.txt(某些文本文件)

AAEJEY CONSUMER COMPANY                    61469 HH13811 4796000501758   NILMA LIQUID BLUE                240 75ML         960.00  20131002
EVERGREEN MARKETING                        61485 PC21946 3014260818685   ORALB 7 BENEFITS T/BRUSH          12 EACH         120.00  20131002
HARISCHANDRA MILLS PLC                     61488 BV50201 4792083040122   HARISCHANDRA COFFEE               40 50GR        4000.00  20131002

在'COMPANY'和'61469'之间,空间长度可能会逐行变化。 我想将该行拆分为以下内容。

  

AAEJEY消费品公司

     

61469

     

HH13811

     

4796000501758

     

NILMA LIQUID BLUE

     

240

     

75ML

     

960.00

     

20131002

这是我的代码,它将所有空间拆分,但我不能将公司名称(AAEJEY CONSUMER COMPANY)作为单个名称或项目名称(NILMA LIQUID BLUE)作为单个名称。

Dim myArray() As String, delimiter As Char = " "
        Dim strBuild As String = ""
        Dim b As Boolean = False
        Dim i As Integer = 0
        Try
            Using sr As New StreamReader(fileName)
                Dim line As String
                While Not sr.EndOfStream
                    line = sr.ReadLine()
                    Console.WriteLine(line)
                    myArray = line.Split(delimiter)
                    Dim order As New OrdData()
                    For index As Integer = 0 To myArray.Length - 1
                        If myArray(index) = "" Then
                            i = index
                            myArray.Skip(1)                                
                        Else                               
                            strBuild += myArray(index) + " "
                            Console.WriteLine(strBuild)
                        End If

                    Next
                End While
            End Using
        Catch e As Exception
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(e.Message)
        End Try

2 个答案:

答案 0 :(得分:2)

看起来你有一个固定长度的文件格式,所以你应该按字符数来计算,例如

line = sr.ReadLine()
var name = line.Substring(0, 43).Trim();
var number = line.Substring(44, 5).Trim();

您的文件没有任何分隔符。您不能使用空格,因为空格是项目的一部分(第一列)。

答案 1 :(得分:2)

您可以尝试这种方便的功能方法。

首先定义一个递归分割行的函数:

Dim f As Func(Of String, IEnumerable(Of Integer), IEnumerable(Of String)) = Nothing
f = Function(t, ns)
    If ns.Any() Then
        Dim n = ns.First()
        Dim i = System.Math.Min(n, t.Length)
        Dim t0 = t.Substring(0, i)
        Dim t1 = t.Substring(i)
        Return New List(Of String) From { t0.Trim() }.Concat(f(t1, ns.Skip(1)))
    Else
        Return New List(Of String) From { t.Trim() }
    End If
End Function

然后像这样定义你的分裂:

Dim splits = { 43, 6, 8, 16, 31, 6, 10, 11 }

现在您可以像这样运行它:

Dim fields = f(line, splits).ToArray()

鉴于您的第一行数据,我得到了这个结果:

result