I got a textbox which holds a persons address
For example
Streetname / house number / box number
They are seperated by a space

I want them to go into their own textboxes TxtStreet, TxtHousNr, TxtBoxNr
I am pretty sure its best done with indexof
But i cannot get it figured out.

Can anybody please give me a exaple?
Thanks

When you say

For example
Streetname / house number / box number

You are not actually giving an example. Do you actually mean that the format is like

Main Street/1234/57

Without knowing what formats are valid I can't show you how to parse it. Typical addresses might be

5B-1017 Cornet St.
108 General Brock Dr.
Box 117 Mayberry

Parsing on blanks is not an option because the different fields can not be determined by blanks alone. Typically, a residence will also not have both a house number and a box number.

Well its a bit different in the USA.
In Europe we use street name / house number / appartment number(if its a appartment or something alike).
So lets say Main street 230 5(230 being house number, 5 being appartment number).
The tricky part also is sometimes a streetname could have 1 or more spaces.
Like lets say something like Martin Luther King Jr. Boulevard.
Your going to have 4 spaces before your actually getting a number.

Hope this makes somewhat sense.

In that case it cannot be parsed by an algorithm. You have to have some absolute way of determining where the various components begin and end. You can't detect based on a digit because you may have a comoponent like 5th Ave. What about if we tokenize based on blanks? That way the above would parse out as

Main
Street
230
5

We could start at the first token and keep grabbing as long as the entire token is not numeric. The first all-numeric would be the house number and if there was another numeric following that it would be the apartment number. Would that work in all cases?

Yeah that would work, also the only option i think.

Being trying a bit, but a example would help if possible.

As a first approximation

        Dim street As String = ""
        Dim number As String = ""
        Dim apt As String = ""
        Dim foundnum As Boolean = False

        For Each token As String In TextBox1.Text.Split()
            If IsNumeric(token) Then
                If foundnum Then
                    apt &= " " & token
                Else
                    foundnum = True
                    number = token
                End If
            Else
                street &= " " & token
            End If
        Next

        TextBox2.Text = "STREET: " & Trim(street) & vbCrLf &
                        "NUM:    " & Trim(number) & vbCrLf &
                        "APT:    " & Trim(apt)

Ok thanks, never coded anything like this.
Gave it a try already, but need to change a few things.
Right now TxtNum.text(Textbox2.text) is showing Street, Num, Apt.
But i need to change it so
TxtStreet.text shows just the street.
TxtNum.text shows just the number.
TxtApt.text shows just the appartment.

Sorry i opened this back, did not want to create something new for this small problem.

I was testing my code with some cards which had different data on them.
Came to the conclusion there is 2 formats for people who live in apartments.

I am guessing the newer format is streetname housenumber aptnumber.

But there seems to be a older format which uses streetname housenumber (/apt) followed by appartmentnumber(no spaces between the number and /apt)

Is there a simple solution for this?

Ok i am going to show you a dutch format, which most of them are anyway.
I got 2 person from the same building with different formats.

Van Derlaan 23 /bu10 (old format)

Van Derlaan 23 3 (new format) which my code works with.

Van Derlaan would be streetname
23 house number
/bu10 would be the apartment number

In both cases, once you have the street portion, the remainder is STREET# and APT#. You could do the switch based on the detection of the first numeric field. Anything following that is the apartment number.

    Dim street As String = ""
    Dim number As String = ""
    Dim apt As String = ""
    Dim foundnum As Boolean = False

    For Each token As String In TextBox1.Text.Split()
        If IsNumeric(token) Then
            If foundnum Then
                apt = token
            Else
                number = token
                foundnum = True
            End If
        Else
            If foundnum Then
                apt = token
            Else
                street &= " " & token
            End If
        End If
    Next

Edited 4 Years Ago by Reverend Jim

Thanks, that was not that hard.
Hoping there is no other format, seen some people claim there is also a chance i could see something like this.
Van Derlaan 23c c being the apartment letter.

I can remove the /bu by just adding that code behind the Apt code right?

` TextBox2.Text = "STREET: " & Trim(street) & vbCrLf &
"NUM: " & Trim(number) & vbCrLf &
"APT: " & Trim(apt)`

You can remove the bu by apt.Replace("/bu","")

As for 23c, unfortunately with either code it will be taken as part of the street name unless you change the code that detects the street number from "first all numeric field" to "first field that starts with a digit".

Alright thanks, i should be able to get that working the way needed.
I'll figure out first if the 23c is actually used or not before changing things up.

Thanks again Jim

This question has already been answered. Start a new discussion instead.