Extrapolating Multiple Data Fields from a String

mpappert

Regular
Joined
Oct 5, 2002
Messages
58
Location
Ottawa, ON, Canada
I'm just wondering if there is a .NET method for doing this tedious task ... I have data set (obtained from swiping a magnetic card) that is stored in a single string ...

The data is separated sometimes by actual field length, and in some instances by separators. I am using code like this to parse the data and was wondering if there was a better .NET method for handling strings in this manner ...

TIA!
M.

Visual Basic:
        ' Card Number
        m_strCardNumber = Mid$(strSwipeData, 9, 10)

        ' First, Middle, Last from "^Last/First Middle^"
        ' Last ends at a '/', First and Middle separated by Space
        ' enclosed in '^' (carots)
        For iLoop = 1 To Len(strSwipeData)
            Select Case Mid$(strSwipeData, iLoop, 1)
                Case "^"
                    If intNameLOC_Begin = 0 Then
                        intNameLOC_Begin = iLoop + 1
                    Else
                        intNameLOC_End = iLoop + 1
                        Exit For
                    End If
                Case "/"
                    intNameLOC_FirstName = iLoop + 1
                Case " "
                    intNameLOC_MiddleName = iLoop + 1
            End Select
        Next iLoop
        m_strName_Last = Mid$(strSwipeData, intNameLOC_Begin, intNameLOC_FirstName - intNameLOC_Begin - 1)
        m_strName_First = Mid$(strSwipeData, intNameLOC_FirstName, intNameLOC_MiddleName - intNameLOC_FirstName - 1)
        m_strName_Middle = Mid$(strSwipeData, intNameLOC_MiddleName, intNameLOC_End - intNameLOC_MiddleName - 1)
 
Check out the tutorial on regular expression matching. You can define a regular expression that matches your string and use the Matches object to get each piece handed to you (First, Middle and Last).

-Nerseus
 
The first thing I thought of using was regular expressions, but a real simple a fast approach would be to use the Split function...
Visual Basic:
        strSwipeData = "^Smith/John myMid^"

        strSwipeData = strSwipeData.Replace("^", "")

        Dim temp() As String
        Dim temp2() As String

        temp = strSwipeData.Split("/"c)
        temp2 = temp(1).Split(" "c)

        m_strName_Last = temp(0)
        m_strName_First = temp2(0)
        m_strName_Middle = temp2(1)
 
I like the regular expression approach .. there are a total of 17 fields that are stored in this magnet swipe string ... I'm guessing I have to write one regular expression that matches the format of the string, but then how do I use the match function to seek the data in the string? The match function searches for data matches (if I use that example Nerseus posted earlier) it looks for "S" ... not everything has a separator .. some information (at the beginning and end) is size delimited (ie. fixed length fields) ... Can this be parsed using RegEx?

M.
 
Back
Top