Hi @StewartBW ,
You can use a state machine approach to properly detect and parse the fields, even if they span multiple lines.
Dim inNote As Boolean = False
Dim inKey As Boolean = False
For Each line As String In lines
If line.StartsWith("NOTE") Then
inKey = False
inNote = True
noteBuilder.Append(line.Substring(line.IndexOf(":"c) + 1).Trim())
ElseIf line.StartsWith("KEY") Then
inNote = False
inKey = True
keyBuilder.Append(line.Substring(line.IndexOf(":"c) + 1).Trim())
ElseIf line.StartsWith("END:VCARD") Then
inNote = False
inKey = False
ElseIf line.StartsWith("BEGIN:VCARD") Then
inNote = False
inKey = False
ElseIf line.StartsWith("VERSION:") Then
inNote = False
inKey = False
ElseIf line.StartsWith("N;LANGUAGE") Then
inNote = False
inKey = False
ElseIf line.StartsWith("FN:") Then
inNote = False
inKey = False
ElseIf line.StartsWith("ORG:") Then
inNote = False
inKey = False
ElseIf line.StartsWith("REV:") Then
inNote = False
inKey = False
Else
If inNote Then
noteBuilder.Append(line.Trim())
End If
If inKey Then
keyBuilder.Append(line.Trim())
End If
End If
Next
Uses DateTime.TryParseExact to parse the REV string using the exact format provided.
Dim revString As String = "20240623T081758Z"
Dim revDate As DateTime
If DateTime.TryParseExact(revString, "yyyyMMdd'T'HHmmss'Z'",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal,
revDate) Then
Console.WriteLine("Parsed Date: " & revDate.ToString("yyyy-MM-dd HH:mm:ss"))
Else
Console.WriteLine("Failed to parse date.")
End If
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.