How to extract the last word in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roblee
    New Member
    • Mar 2011
    • 2

    How to extract the last word in a string?

    I've imported a CSV file into my Access Database and the Address field also contains a City name, ie:
    123 SE Main St. #A Seattle
    8235 127th St Ct E Tacoma
    47 Wilson River Highway Unit H1 Bremerton

    I want to parse the City into a new field and then delete the city name from the address field. I can use this expression in a query to parse to the firstspace:

    Expr: Left([mailaddr1],InStr([mailaddr1]," "))

    but is there a function which will easily allow me to extract the last word in a string?
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    You can use Right and InStrRev.
    TheSmileyCoder

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Here is a Custom Function that I created for you, which will do what you request. The Function Definition, Sample Calls, as well as Output are listed below:
      Code:
      Public Function fExtractLastWord(strWord As String) As Variant
      Dim varRet As Variant
      
      varRet = Split(strWord, " ")
      
      fExtractLastWord = varRet(UBound(varRet))
      End Function
      Code:
      Debug.Print fExtractLastWord("8235 127th St Ct E Tacoma")
      Tacoma
      Code:
      Debug.Print fExtractLastWord("47 Wilson River Highway Unit H1 Bremerton")
      Bremerton
      Code:
      Debug.print fExtractLastWord("Help")
      Help
      P.S. - Here is another alternative, but you must have at least 1 Space in the String or an Error will occur:
      Code:
      Debug.Print Mid$("<String Here>", InstrRev("<String Here>"," "))

      Comment

      • OldBirdman
        Contributor
        • Mar 2007
        • 675

        #4
        That will not work for Walla Walla, Port Angeles, or Saint Helens. Even the example "8235 127th St Ct E Tacoma" could be mis-parsed if E Tacoma were a city.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          @OldBirdman - There will definitely be Records that fall through the crack, but there is no method to differentiate which Strings comprise various Cities, unless of course, you incorporate some kind of Lookup Table into the Logic. A Table consisting of all City Names that consist of 2 or more syllables would do the trick.

          Comment

          • OldBirdman
            Contributor
            • Mar 2007
            • 675

            #6
            @ADezii - I'm not critizing your methods. I was pointing out to original poster that this, or any, function cannot determine the city name from its position in the string only.

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              @OldBirdman - You are absolutely correct in your statements. This is obviously something that I should have indicated to the OP along with the Code. Thanks for catching my oversight.

              Comment

              Working...