How do I extract an address from a text field using vb script with string?

3491
3
01-21-2016 09:39 AM
CaitlinChristenson1
New Contributor

I have the address of 101 Main st. All I want is the street up until the space to be extract so just the main st portion for all of my address using the field calculator -> VB Script -> type: string.

0 Kudos
3 Replies
WesMiller
Regular Contributor III
0 Kudos
JoeBorgione
MVP Emeritus

Used to do that in vb, but now I do it in python.

In VB you can use the right right() function.  You could also use a split() function but with multi word street names it gets tricky to glue them back together.  Here is how I like to do it in python (with credit to Darren Wiens​)

All you need to do with this is adjust the return statement at the end to return which ever variable you want...

def myParser(inString):

  splitString = inString.split(' ')   ## split address

  a = splitString[0]                  ## house number

  b = splitString[1]                  ## predir

  c = ' '.join(splitString[2:-1])     ## join back together the #2 element and everthing before the -1(last) element

  d = splitString[-1]                 ## suf dir or s type

  return c                            ## this one returns the street name for example main or joes view

myParser(!YourAddressField!)

That should just about do it....
0 Kudos
RobertBorchert
Frequent Contributor III

Several ways to go about this.

Now do you simply want to strip out the numerical portion of the address and store it in a new attribute or do you simply want to get rid of it.?

I will assume you want to preserve the numerical portion of the address.

This part will allow you to populate the new Number attribute

left( [servAddr1], (instr(1, [servAddr1]," ")-1))

what this does is takes the left portion of the full address up to the first space and populate just that.  The -1 is critical or it will copy the space as well.

for the rest of it this formula will take everything but the number.  I populated servAddr2 with the number

mid( [servAddr1], (len( [servAddr2])+2),50)

In this case it will populate the street portion with everything after the space after the number.  It will take the length of your address portion  and start there and put in everything after that. The +2 is important so it will start 2 spaces after the end of the number and not include the space.

it actually takes a lot longer to write this than it does to do it.   Just substitute servAddr1 with your attributes.