Remove the last word from values in a string field via Avenue

Last Published: April 25, 2020


The sample script in this article removes the last word from values in a string field. The script creates a new field and populates it with the new string values. For example, if a table contains a County name field with values: San Diego County, Ventura County, Jeff H. Davis County, and so forth; the script will create a new field with the values: San Diego, Ventura, Jeff H. Davis, and so forth.


  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the new window:

    '-- Script: lastword.ave
    '-- This script strips the last word from values in a string field.

    TheTable = av.GetActiveDoc
    theVTab = theTable.GetVTab
    theFieldList = theVTab.GetFields
    theOldField = MsgBox.List(theFieldList,
    "Choose a field which you want the last word deleted","")
    theNewField = Field.Make("County2", #FIELD_CHAR, 30, 0)
    theRecCount = theVTab.GetNumRecords
    for each rec in theVTab
    av.SetStatus((rec/theRecCount) * 100)
    inputString = theVTab.ReturnValueString(theOldField, rec)
    inputStringWordCount = inputString.AsList.Count
    if (inputStringWordCount < 2) then
    theVTab.SetValueString(theNewField, rec, inputString)
    outputString = ""
    for each word in 0..(inputStringWordCount - 2)
    outputString = outputString ++ inputString.Extract(word)
    outputString = outputString.Trim
    theVTab.SetValueString(theNewField, rec, outputString)

    '-- End Script: lastword.ave

  3. Attach the script to a button on the Table GUI.

    A. Compile the script.
    B. Switch to the Project window.
    C. Select Customize from the Project menu.
    D. Select Table under the Type dropdown on the Customize dialog.
    E. Select Buttons under Category.
    F. Click the New button.
    G. Double-click the Click property in the Customize dialog box.
    H. Enter the name of the script in the Script Manager and click Select.
    I. Close the Customize dialog box.

    For more information, see 'Customize dialog box' in ArcView Help.

  4. Open the table and click the new button.
  5. Select the string field to process and click OK.

Article ID:000004709

  • Legacy Products

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options