Vb.net 有多少个空格?你可以试着打破它@蒂科布:没有分隔符,这就是为什么它这么难的原因。否则它将是一个简单的String.Parse()。但它是位置格式的。因此,列的大小总是固定的。所有的列都有固定数量的位置。那实际上没那么糟糕。比什么都单调。给我一些。我将写一个

Vb.net 有多少个空格?你可以试着打破它@蒂科布:没有分隔符,这就是为什么它这么难的原因。否则它将是一个简单的String.Parse()。但它是位置格式的。因此,列的大小总是固定的。所有的列都有固定数量的位置。那实际上没那么糟糕。比什么都单调。给我一些。我将写一个,vb.net,parsing,Vb.net,Parsing,有多少个空格?你可以试着打破它@蒂科布:没有分隔符,这就是为什么它这么难的原因。否则它将是一个简单的String.Parse()。但它是位置格式的。因此,列的大小总是固定的。所有的列都有固定数量的位置。那实际上没那么糟糕。比什么都单调。给我一些。我将写一个我几年前为此所做的小例子。@TyCobb:给我几分钟时间,我将制作一个示例文件并粘贴它。我只需要知道如何做到这一点,以便在以后需要时可以添加更多的专栏,并真正学到一些东西。:)我不需要样本文件。只要给你一个基于粘贴内容的示例就可以了,根据你的描



有多少个空格?你可以试着打破它@蒂科布:没有分隔符,这就是为什么它这么难的原因。否则它将是一个简单的
String.Parse()
。但它是位置格式的。因此,列的大小总是固定的。所有的列都有固定数量的位置。那实际上没那么糟糕。比什么都单调。给我一些。我将写一个我几年前为此所做的小例子。@TyCobb:给我几分钟时间,我将制作一个示例文件并粘贴它。我只需要知道如何做到这一点,以便在以后需要时可以添加更多的专栏,并真正学到一些东西。:)我不需要样本文件。只要给你一个基于粘贴内容的示例就可以了,根据你的描述,它应该适合你的需要。当然可以。现在我来看看Ryan的答案。好的,TyCobb,我也认为它更干净了。@MDTech.us_MAN这应该是你真正想要使用的答案。我不知道VB有这样的课程。你的答案看起来也很有趣,在某些情况下可能很有用。顺便说一句,在大字符串上使用子字符串将在LOH上泄漏,请使用对stringSure的索引访问。现在我来看看Ryan的答案。好的,TyCobb,我也认为它更干净了。@MDTech.us_MAN这应该是你真正想要使用的答案。我不知道VB有这样的课程。你的答案看起来也很有趣,在某些情况下可能很有用。顺便说一句,在大字符串上使用子字符串将在LOH上泄漏,使用对字符串的索引访问可以将字符串用作输入吗?@MDTech.us_MAN@TyCobb:哦,对了。我没有时间开始阅读课堂文档。字符串可以用作输入吗?@MDTech.us_MAN@TyCobb:哦,对了。我没有时间开始阅读课堂上的文档。
Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\TestFolder\test.log")
  Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
  Reader.SetFieldWidths(5, 10, 11, -1)

  Dim currentRow As String()
  While Not Reader.EndOfData
    Try
      currentRow = Reader.ReadFields()
      Dim currentField As String 
      For Each currentField In currentRow
        MsgBox(currentField)
      Next 
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
      MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
    End Try
  End While 
End Using
Public Class Person

    Public Property FirstName As String
    Public Property LastName As String
    Public Property Email As String

End Class

Public Class ColumnHandler

    Public Property Process As Action(Of Person, String)

    Public Property Length As Integer

    Public Sub New(processAction As Action(Of Person, String), columnLength As Integer)
        Process = processAction
        Length = columnLength
    End Sub

End Class

Module Module1

    Sub Main()

        Dim columnHandlers() As ColumnHandler =
            {
                New ColumnHandler(Sub(p, s) p.FirstName = s, 10),
                New ColumnHandler(Sub(p, s) p.LastName = s, 10),
                New ColumnHandler(Sub(p, s) p.Email = s, 16)
            }

        Dim fileLines() As String =
            {
                "John      Doe       john@example.com",
                "Ty        Cobb      anon@example.com"
            }

        Dim people As New List(Of Person)

        For Each line As String In fileLines

            Dim currentPosition As Integer = 0
            Dim person As New Person()

            For Each columnHandler As ColumnHandler In columnHandlers
                columnHandler.Process.Invoke(person, line.Substring(currentPosition, columnHandler.Length).Trim())
                currentPosition += columnHandler.Length
            Next

            people.Add(person)

        Next

        Console.WriteLine(people.Count.ToString())
        Console.WriteLine(people(1).LastName)
        Console.ReadLine()

    End Sub

End Module
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

void Main()
{
    var columnHandlers = new[]
    {
        new {Process = new Action<Person, string>((p, s) => p.FirstName = s), Length = 10},
        new {Process = new Action<Person, string>((p, s) => p.LastName = s), Length = 10},
        new {Process = new Action<Person, string>((p, s) => p.Email = s), Length = 16},
    };

    //Replace this with a stream or however you were going to get/read the lines
    var fileLines = new[]
    {
        "John      Doe       john@example.com",
        "Ty        Cobb      anon@example.com",
    };

    var people = new List<Person>();

    foreach (var line in fileLines)
    {
        var currentPosition = 0;
        var person = new Person();
        foreach (var columnHandler in columnHandlers)
        {
            columnHandler.Process(person, line.Substring(currentPosition, columnHandler.Length).Trim());
            currentPosition += columnHandler.Length;
        }

        people.Add(person);
    }

    Console.WriteLine(people.Count.ToString());
    Console.WriteLine(people[1].LastName);
}