How to find a parameter in multiple line string

Posted on 2011-10-25
Last Modified: 2012-05-12

I have this string from a GPS serial input

bufferGPS = 
"$GPDTM,W86,,00.0000,N,00.0000,E,,W84*41" & vbCrLf
$GPGGA,086610.000,3900.6474,N,12141.4574,E,1,8,1.6,60,M,,M,,*61" & vbCrLf
$GPRMC,086609,00,A,3900,6471,N,12141.4578,E,0.0,270.0,071011,8.2,W,A*2B" & vbCrLf
$GPVTG,276.0,0,T,278.2,M,0.0,N,0.0,K,A*29" & vbCrLf
$GPVDR,,,,,,*57" & vbCrLf
$GPZDA,086609.00,07,10,2011,-08,00*45" & vbCrLf"

Open in new window

If I want to extract the first parameter of $GPGGA which is 086610.000, the quick and easy way I know how to do this is

dim temp as String() = bufferGPS.Split(","c)
Dim ggaTime As String = temp(8)

Open in new window

However, because later the end user might alter the GPS strings' sequence, I would like to have a parsing code that searches the $GPGGA string automatically in a host of other GPS strings.

Being a vb6 gal with no experience with (crap), I ported the below code from vb6.  However after solving a lot of issues like (crap) no longer support left() and len(), the code still could not work, and I am in a offshore environment with not much time to look into this.
"Public Sub ParseSentence(ByVal InBuff As String, ByVal MyGGA As GGA, ByVal Myvtg As VTG, ByVal MyGSA As GSA, ByVal MyGSV As GSV)

        Dim intcomma As Integer
        Dim aryData(25)
        Dim x, z, st, fn, intst As Integer
        Dim strSentence, strTemp, strDummy As String

        '** Find the start of the sentence...locate $... st stores start position.
        For x = 1 To InBuff.Length()
            If InBuff.Substring(x, 1) = "$" Then
                st = x
                Exit For
            End If

        '** Find the end of the sentence...locate CR... fn stores end position.
        For x = st To InBuff.Length()
            If InBuff.Substring(x, 1) = vbCr Then
                fn = x
                Exit For
            End If

        '** Use st and fn to slice the string to a sentence.
        strSentence = InBuff.Substring(st, fn - st)

        '** How many fields are there in the sentence...Count the comma's
        For x = 1 To strSentence.Length()
            If strSentence.Substring(x, 1) = "," Then
                intcomma = intcomma + 1
            End If

        '** Fill an array with the field data..
        z = 8
        For x = 1 To intComma
            Do While strSentence.Substring(z, 1) <> "," And strSentence.Substring(z, 1) <> "*"
                aryData(x) = aryData(x) & strSentence.Substring(z, 1)
                z = z + 1
            z = z + 1
        Select Case strSentence.Substring(0, 6)
            Case "$GPGGA"
                MyGGA.strUttcTime = aryData(1)
                MyGGA.varLatitude = aryData(2)
                MyGGA.strNSIndicator = aryData(3)
                MyGGA.varLongitude = aryData(4)
                MyGGA.strEWIndicator = aryData(5)
                MyGGA.strPositionFix = aryData(6)
                MyGGA.strSatsUsed = aryData(7)
                MyGGA.strIIDOP = aryData(8)
                MyGGA.strAltitude = aryData(9)
                MyGGA.strAltUnits = aryData(10)
                MyGGA.strGeoid = aryData(11)
                MyGGA.strSepUnits = aryData(12)
                MyGGA.strDgpsAge = aryData(13)
                MyGGA.strDgpsid = aryData(14)
            Case "$GPVTG"
                Myvtg.strCourse1 = aryData(1)
                Myvtg.strReference1 = aryData(2)
                Myvtg.strCourse2 = aryData(3)
                Myvtg.strReference2 = aryData(4)
                Myvtg.strSpeed1 = aryData(5)
                Myvtg.strSpeedUnit1 = aryData(6)
                Myvtg.strSpeed2 = aryData(7)
                Myvtg.strSpeedUnit2 = aryData(8)
            Case "$GPGSA"
                MyGSA.strMode = aryData(1)
                MyGSA.strFixType = aryData(2)
                MyGSA.strSat1 = aryData(3)
                MyGSA.strSat2 = aryData(4)
                MyGSA.strSat3 = aryData(5)
                MyGSA.strSat4 = aryData(6)
                MyGSA.strSat5 = aryData(7)
                MyGSA.strSat6 = aryData(8)
                MyGSA.strSat7 = aryData(9)
                MyGSA.strSat8 = aryData(10)
                MyGSA.strSat9 = aryData(11)
                MyGSA.strSat10 = aryData(12)
                MyGSA.strSat11 = aryData(13)
                MyGSA.strSat12 = aryData(14)
                MyGSA.strPDOP = aryData(15)
                MyGSA.strHDOP = aryData(16)
                MyGSA.strVDOP = aryData(17)
            Case "$GPGSV"
                MyGSV.StrNoMessages = aryData(1)
                MyGSV.StrSeq = aryData(2)
                MyGSV.StrSatsinview = aryData(3)
                MyGSV.StrSatId1 = aryData(4)
                MyGSV.StrElevation1 = aryData(5)
                MyGSV.StrAzimuth1 = aryData(6)
                MyGSV.StrSNR1 = aryData(7)
                MyGSV.StrSatId2 = aryData(8)
                MyGSV.StrElevation2 = aryData(9)
                MyGSV.StrAzimuth2 = aryData(10)
                MyGSV.StrSNR2 = aryData(11)
                MyGSV.StrSatId3 = aryData(12)
                MyGSV.StrElevation3 = aryData(13)
                MyGSV.StrAzimuth3 = aryData(14)
                MyGSV.StrSNR3 = aryData(15)
                MyGSV.StrSatId4 = aryData(16)
                MyGSV.StrElevation4 = aryData(17)
                MyGSV.StrAzimuth4 = aryData(18)
                MyGSV.StrSNR4 = aryData(19)

        End Select

Open in new window

Without knowing the full capability of functions, my question:  is there a elegant and simple way or function in that automatically search for "$GPGGA" in multiple lines strings; I dont know, maybe some simple code like below:

If bufferGPS.SuperFunctionSubstring(0, 6) = "$GPGGA" Then
                        Dim temp As String() = bufferGPS.Split(","c)
                       Dim templong As String = temp(2) ' Long
                        Dim tempN As String = temp(3) ' N
                        Dim templat As String = temp(4) ' Lat
                       Dim tempE As String = temp(5) ' E
end if

Open in new window

Question by:Heidi_Lee
    LVL 83

    Accepted Solution

    You can try

    Dim StartIndex as Integer = bufferGPS.IndexOf("$GPGGA")

    Dim gpgga as String = bufferGPS.SubString(bufferGPS.IndexOf(",", StartIndex) + 1, bufferGPS.IndexOf(",", bufferGPS.IndexOf(",", StartIndex) ) -  bufferGPS.IndexOf(",", StartIndex) )

    This is untested so you may have to tweak it.
    LVL 7

    Assisted Solution

    An extension of your original idea, along with using Array.IndexOf to get at what you need.  This seems to work:
    Dim bufferGPS As String
    bufferGPS = "$GPDTM,W86,,00.0000,N,00.0000,E,,W84*41" & vbCrLf & _
                "$GPGGA,086610.000,3900.6474,N,12141.4574,E,1,8,1.6,60,M,,M,,*61" & vbCrLf & _
                "$GPRMC,086609,00,A,3900,6471,N,12141.4578,E,0.0,270.0,071011,8.2,W,A*2B" & vbCrLf & _
                "$GPVTG,276.0,0,T,278.2,M,0.0,N,0.0,K,A*29" & vbCrLf & _
                "$GPVDR,,,,,,*57" & vbCrLf & _
                "$GPZDA,086609.00,07,10,2011,-08,00*45" & vbCrLf
    Dim temp As String() = bufferGPS.Split(New Char() {","c, CChar(vbCr), CChar(vbLf)})
    Dim ggaTime As String
    Dim ggaIndex As Integer = Array.IndexOf(temp, "$GPGGA")
    If ggaIndex > -1 Then 
        ggaTime = temp(ggaIndex + 1)
    End If
    ' Or if you do not need all of the empty entries try this: '
    Dim temp As String() = bufferGPS.Split(New Char() {","c, CChar(vbCr), CChar(vbLf)}, StringSplitOptions.RemoveEmptyEntries)

    Open in new window

    LVL 13

    Assisted Solution

    regular expressions are very versatile and work well  in these types of situations.  I use Expresso to build and test them it has served me well over the years
    Dim matchString as string = "(?im-snx)(?<GPGGA>[$]GPGGA.*?)\r?$"
            Dim m As System.Text.RegularExpressions.Match
            m = System.Text.RegularExpressions.Regex.Match(bufferGPS, matchString)
            Dim GPGGA As String
            Dim CSV() As String
            Dim GPGGA1 As String
                GPGGA = m.Groups("GPGGA").Value
                CSV = GPGGA.Split(New String() {","}, StringSplitOptions.None)
                GPGGA1 = CSV(1)
            Catch ex As Exception
            End Try

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now