Jump to content

Does anyone know how to parse an address from a text field?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
TheWebGuy

TheWebGuy

    Newbie

  • Members
  • Pip
  • 7 posts
I want to parse an address from a text field.
for example

textbox data = 123 test street, mountain view, Ca 91302

Dim address = Address.text

parse(address)

to
Address1
Address2
City
State
Zip


I found someones sample code, but it doesn't work all that well.

here it is non the less



 Public Function parseAddress(ByVal input As String) As Collection

        input = input.Replace(",", "")

        input = input.Replace("  ", " ")

        Dim splitString() As String = Split(input)

        Dim streetMarker() As String = New String() {"street", "st", "st.", "avenue", "ave", "ave.", "blvd", "blvd.", "highway", "hwy", "hwy.", "box", "road", "rd", "rd.", "lane", "ln", "ln.", "circle", "circ", "circ.", "court", "ct", "ct."}

        Dim address1 As String

        Dim address2 As String = ""

        Dim city As String

        Dim state As String

        Dim zip As String

        Dim streetMarkerIndex As Integer


        zip = splitString(splitString.Length - 1).ToString()

        state = splitString(splitString.Length - 2).ToString()

        streetMarkerIndex = getLastIndexOf(splitString, streetMarker) + 1

        Dim sb As New StringBuilder


        For counter As Integer = streetMarkerIndex To splitString.Length - 3

            sb.Append(splitString(counter) + " ")

        Next counter

        city = RTrim(sb.ToString())

        Dim addressIndex As Integer = 0


        For counter As Integer = 0 To streetMarkerIndex

            If IsNumeric(splitString(counter)) _

            Or splitString(counter).ToString.ToLower = "po" _

            Or splitString(counter).ToString().ToLower().Replace(".", "") = "po" Then

                addressIndex = counter

                Exit For

            End If

        Next counter


        sb = New StringBuilder

        For counter As Integer = addressIndex To streetMarkerIndex - 1

            sb.Append(splitString(counter) + " ")

        Next counter


        address1 = RTrim(sb.ToString())


        sb = New StringBuilder


        If addressIndex = 0 Then

            If splitString(splitString.Length - 2).ToString() <> splitString(streetMarkerIndex + 1) Then

                For counter As Integer = streetMarkerIndex To splitString.Length - 2

                    sb.Append(splitString(counter) + " ")

                Next counter

            End If

        Else

            For counter As Integer = 0 To addressIndex - 1

                sb.Append(splitString(counter) + " ")

            Next counter

        End If

        address2 = RTrim(sb.ToString())


        Dim output As New Collection

        output.Add(address1, "Address1")

        output.Add(address2, "Address2")

        output.Add(city, "City")

        output.Add(state, "State")

        output.Add(zip, "Zip")

        Return output

    End Function


    Private Function getLastIndexOf(ByVal sArray As String(), ByVal checkArray As String()) As Integer

        Dim sourceIndex As Integer = 0

        Dim outputIndex As Integer = 0

        For Each item As String In checkArray

            For Each source As String In sArray

                If source.ToLower = item.ToLower Then

                    outputIndex = sourceIndex

                    If item.ToLower = "box" Then

                        outputIndex = outputIndex + 1

                    End If

                End If

                sourceIndex = sourceIndex + 1

            Next

            sourceIndex = 0

        Next

        Return outputIndex

    End Function




#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
U want to do something like ParseInt , ParseFloat to obtain ParseAddress is it?
it is better off u construct a custom data type or create a method to return the address in the format you want

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,083 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If the textbox will always contain stuff in the format of "123 test street, mountain view, Ca 91302", you can just split it on the comma. And for Ca 91302 you'd have to check for digits.
Imo it'll be easier to just have different textboxes




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users