Field Calculator- Delete last 3 text characters

44195
6
10-16-2011 04:56 AM
RobertLea
New Contributor II
Hello,

I have postcodes which I need to cut down from say BN14 5RY to only BN14. I cannot simply use the 'left(input, n)' equation as some postcodes are only 6 figures long such as BN1 5RY and I always need the first 3/4 characters.

Does anyone know a way to delete the last 3 characters?

Thanks in advance.
6 Replies
RichardFairhurst
MVP Honored Contributor
Hello,

I have postcodes which I need to cut down from say BN14 5RY to only BN14. I cannot simply use the 'left(input, n)' equation as some postcodes are only 6 figures long such as BN1 5RY and I always need the first 3/4 characters.

Does anyone know a way to delete the last 3 characters?

Thanks in advance.


You can use the left() function if you combine it with the len() function.  This will do what you want (I assumed you also want to get rid of the space character and not just the last three non-whitespace characters):

left(input, len(input) - 4)

or

rtrim(left(input, len(input) - 3))

The len() function is normally used in combination with the left(), mid() and right() functions to control starting points, ending points and number of character values when doing string parsing.
RichardFairhurst
MVP Honored Contributor
Just in case you ever need to use the field calculation in a Python script, to do the same thing in Python you would also use the len() function.  For example:

!input![len(!input!)-4]

or

rstrip(!input![len(!input!)-3])

(Keep in mind that whitespace characters count, so in your examples "BN14 5RY" is 8 characters long, not 7 and "BN1 5RY" is 7 characters long, not 6.)
0 Kudos
RobertLea
New Contributor II
Great thank you for all that information.

Yes I made sure to delete the white space first and I was able to use the len() funtion with Left() to get the postcodes trimmed to the level I needed.

Thanks again.
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
If your field contains string/text data, there is no need for the len method in Python since string data is an "iterable".  For example
>>> a = "abcdefghijklmnop"
>>> b = a[:-4]
>>> b
'abcdefghijkl'
>>>
RichardFairhurst
MVP Honored Contributor
If your field contains string/text data, there is no need for the len method in Python since string data is an "iterable".  For example
>>> a = "abcdefghijklmnop"
>>> b = a[:-4]
>>> b
'abcdefghijkl'
>>>


Dan:

I am not really a Python programmer and my expression above would not work anyway because I didn't notice (in my quick check of some Python websites) that I needed the colon to indicate to take everything from the beginning of the string to the second position.  Thanks for correcting my Python ignorance and showing another example of where Python is more elegant than VB Script.
0 Kudos
NobbirAhmed
Esri Regular Contributor
If there is always a space between first 3/4 characters and last 3 characters you can split the string and take the first part:

"BN14 5RY".split()[0]
 
# or
 
"BN1 5RY".split()[0]