简体   繁体   中英

VB.NET String split in advance (C# or VB.NET)

This is the text lines,that I want to split,data.txt (some text file)

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

Between 'COMPANY' and '61469' space length may be vary line to line. I want to split that line as following.

AAEJEY CONSUMER COMPANY

61469

HH13811

4796000501758

NILMA LIQUID BLUE

240

75ML

960.00

20131002

This is my code,it split all with space,but i cannot get the Company Name (AAEJEY CONSUMER COMPANY) as a single name or Item Name (NILMA LIQUID BLUE) as a single name.

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

It looks like you have a fixed length file format, so you should actually go by the number of characters, eg

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

Your file doesn't have any delimiter. You cannot use spaces as spaces are part of the items (the first column).

You could try this handy functional approach.

First define a function to recursively split a line:

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

Then define your splits like this:

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

Now you can run it like this:

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

Given your first line of data I got this result:

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM