[英]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) 这是我要拆分的文本行,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
Between 'COMPANY' and '61469' space length may be vary line to line. 在“ COMPANY”和“ 61469”之间,行长度可能会有所不同。 I want to split that line as following.
我想按以下方式拆分该行。
AAEJEY CONSUMER COMPANY
AAEJEY消费者公司
61469
61469
HH13811
HH13811
4796000501758
4796000501758
NILMA LIQUID BLUE
尼玛液体蓝
240
240
75ML
75ML
960.00
960.00
20131002
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. 这是我的代码,它全部用空格分开,但是我不能将公司名称(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
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: 根据您的第一行数据,我得到了以下结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.