Separating TextFile

Asked 2 years ago, Updated 2 years ago, 116 views

I would like to read the file into the folder and then display LISTVIEW. Multiple delimiters in the same line have string separations.
May 15, 2016 21:59:13,739[7]INFO-Login.User_Auto-o03dx1n Unknown-Login: KST028

Day May 15, 2016
     Time 21:59:13,739
     LogType[7]INFO
     MethodName Login.User_Aut
     SessionIDo03dx1n Unknown
     LoginID Login—KST028

Message
I put a space in Split below, but I was able to separate only the date, time, and LogType.Everything else is wrong.
How can I break it off?
Please let me know.Please.

'Files loaded
        OpenFileDialog1.ShowDialog()
        Dim filepath As String = OpenFileDialog1.FileName
        Dim inputstream As New IO. StreamReader (filepath)
        Dim newstr(2) As String
        Dim Day As Integer
        Dim Time As String
        Dim LogType As String
        Dim LoginID As String
        Dim Message As String
        Do While inputstream.Peek<>-1
        US>' delimited
        newstr=inputstream.ReadLine().Split("")

        Day = newstr(0)
        Time = newstr(1)
        LogType=newstr(2)&"&newstr(3)
        LoginID = newstr(6)
        Message = newstr(7)

        US>'Read Selected File
        Me.LogListView.Items.Add (Day)
        Me.LogListView.Items.Item(LogListView.Items.Count-1).SubItems.Add(Time)
        Me.LogListView.Items.Item(LogListView.Items.Count-1).SubItems.Add(LogType)    
        Me.LogListView.Items.Item(LogListView.Items.Count-1).SubItems.Add(Message)
    Loop

    inputstream.Close()
End Sub

vb.net

2022-09-30 20:16

1 Answers

Assume that the file format to be read is the format described in line 3 of the question:

May 15, 2016 21:59:13,739[7]INFO-Login.User_Auto-o03dx1n Unknown-Login: KST028

newstr=inputstream.ReadLine().Split(").Split(")If you put the following code directly underneath, you will see what values are in each array index as a result of the Split.

Split result verification code

' delimited
newstr=inputstream.ReadLine().Split("")
US>'Add code here
Dimsb As New System.Text.StringBuilder
Fori=0 To newstr.Length-1
    sb.AppendFormat("{0,2}:{1}", i, newstr(i)) .AppendLine()
Next
MessageBox.Show(sb.ToString())

Split Results Verification Code Execution Results

 0: May 15, 2016
 1: 21:59:13,739
 2: [7]
 3: INFO
 4: -
 5—Login.User_Aut
 6: -
 7—o03dx1n
 8—Unknown
 9: -
10—Login:
11: KST028

The above results indicate that the newstr(0) value contains May 15, 2016 and the newstr(4) value contains -.
You can see that the provided code deviates from the LogType or later index.

Split in half-width spaces to arrange results separated by all half-width spaces.
Therefore, you will need to re-index the array according to the file format, such as MethodName=newstr(5).


2022-09-30 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.