HOW TO

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

Last Published: April 25, 2020

Summary

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.

Procedure



  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:

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

    TheTable = av.GetActiveDoc
    theVTab = theTable.GetVTab
    theVTab.SetEditable(TRUE)
    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)
    theVTab.AddFields({theNewField})
    theRecCount = theVTab.GetNumRecords
    av.SetStatus(1)
    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)
    else
    outputString = ""
    for each word in 0..(inputStringWordCount - 2)
    outputString = outputString ++ inputString.Extract(word)
    outputString = outputString.Trim
    end
    theVTab.SetValueString(theNewField, rec, outputString)
    end
    end
    theVTab.SetEditable(FALSE)
    av.ClearStatus

    '-- 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

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic