1 year ago

#359558

test-img

G_Hosa_Phat

String.Parse() To Custom Object Using Specified Format

EDITED TO REWORK AND (HOPEFULLY) SIMPLIFY THE QUESTION

I was working on a method for taking a String value and splitting it into its component parts for populating a custom structured object when a question occurred to me: Methods exist for parsing strings into "well-known" objects like System.Net.IPAddress or System.DateTime (e.g., DateTime.Parse(), DateTime.TryParse(), or DateTime.ParseExact()), but these objects use well-defined structures that don't change.

Nothing (at least, that I've been able to find) is available that can "quickly" parse a string into a custom-defined object/structure. Is there a way to achieve the same result by basically "reversing" the functionality of either the base String.Format() method or string interpolation to extract a target object's property/field values from a given string?

For the purposes of this question and my examples, I'll refer to this hypothetical method as String.Parse()

For example, given an object with the following definition:

Public Class LastModified
    Public Property User As String = Nothing
    Public Property FromIP As String = Nothing
    Public Property InterfaceName As String = Nothing
    Public Property Method As String = Nothing
    Public Property ModifiedDate As DateTime = DateTime.MinValue
End Class

I was thinking of something like one of the following pseudocode alternatives:

' PSEUDOCODE - NOT OPERATIONAL - DO NOT USE
Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
Dim LMB As New LastModified

' USING "BASIC" String.Format() SYNTAX
LMBString.Parse("{0} {1}:{2} {3} {4}", 
                LMB.User,
                LMB.FromIP,
                LMB.InterfaceName,
                LMB.Method,
                LMB.ModifiedDate)

' USING STRING INTERPOLATION SYNTAX
LMBString.Parse($"{LMB.User} {LMB.FromIP}:{LMB.InterfaceName} {LMB.Method} {LMB.ModifiedDate}")

Perhaps this functionality is available through a 3rd-party library, and I suppose I could probably build a .Parse() extension method that could achieve this goal relatively easily, but I wanted to go ahead and see if someone else had already invented that particular wheel, and my Google-fu may simply be too weak. As stated above, I've not been able to find implementations of either of these pseudocode operations currently available natively in .NET (I'm using Framework 4.7 for my current project), but I don't mind looking stupid to ask a question that might have a simple solution - especially if it helps someone else who faces the same issues.


Just for reference purposes, in my current implementation, I'm using the following (simplified) code, which is totally functional and provides the results I'm looking for. I'm satisfied to keep using this "as-is", but I couldn't help wondering if there might be a way to achieve the same goal with fewer lines of code.

Public Function ParseStringToLastModifiedObject As LastModified
    Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
    Dim LMBBuffer As String() = LMBString.Split(" "c)
    Dim LMBReturn As New LastModified

    With LMBReturn
        Dim IPBuffer As String() = LMBBuffer(1).Split(":"c)

        .User = LMBBuffer(0) '-> "admin"
        .Method = LMBBuffer(2) '-> "UI"
        .ModifiedDate = CDate(LMBBuffer(3) & " " & LMBBuffer(4)) '-> #3/26/2022 15:43:38#
        .FromIP = IPBuffer(0) '-> "192.168.168.168"
        .InterfaceName = IPBuffer(1) '-> "X0"
    End With

    Return LMBReturn
End Function

.net

vb.net

parsing

string-interpolation

string.format

0 Answers

Your Answer

Accepted video resources