Removing last character from a string using VB

1894
8
05-08-2018 05:47 PM
BryanL
by
New Contributor

Hi All,

I'm trying to remove "." from various string of different length and would be great if anyone could advise or correct me on the right VB code.

Example of the data that I am using are as follows;

Apple.

Orange.

Pear

Salade

Lime.

Some of the string above have a "." at the back while others don't and they are all of different Length. The code I've written below simply take away the last character regardless if they are "." or not. Would be great if anyone could help me with this.

rTrim( Left ([MACodeLongDescription], LEN([MACodeLongDescription]) -1))

Thank y'all in advance.

Tags (2)
0 Kudos
8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Set the python parser in the field calculator.  VB is no longer supported, so begin to learn Python and Arcade

# ---- examples
a = 'Apple.'

a.replace('.', "")
Out[163]: 'Apple'

a = 'Pear'

a.replace('.', "")
Out[165]: 'Pear'

Now for your field calculation you simply need to include the fieldname in ! ie !OriginalData!  and calculate values preferably in a new text field ... like

!OldField!.replace(".", "")  # ---- no spaces between the double quotes
BryanL
by
New Contributor

Hi Dan,

Appreciate the fast reply.

I gave it a go and noticed it didn't work...not too sure if I got it right.

Cheers

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Why aren't you using the field calculator?  (right-click on the field you want to calculate values for and follow on)

In any event

Field name is the field you are calculating into.

The field name used in the expression is the one that contains the old data and it should be different than the field name

0 Kudos
BryanL
by
New Contributor

I'm actually doing this in the model building environment..taking the input field, removing the trailing "." if any and replacing the new values into the original field

Apologies for any confusion caused. Just started exploring ArcGis.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Bryan ... switch the parser to python and set it to string.  It isn't a good idea to replace an existing field, add a new field prior to this step and use it to house the results.

TedKowal
Regular Contributor II

IN VB:

IF right([Field],1) = "." then

   [Field] = Trim(Left([Field],LEN([Field])-1))

ELSE

   [Field]=Trim([Field])

END IF

BryanL
by
New Contributor

Hi Guys,

Thanks for the help. Appreciate it a lot.

I've since managed to get the result I wished for.

Cheers

0 Kudos

Just FYI, another way to remove the right most string in VB is to use the String Reverse method, remove first string using left or mid, then use the string reverse again.

This is a method I have found handy for classifying Schools into Elementary, Junior High, Middle, and High Schools, because a list of schools usually has a format like "MARTIN LUTHER KING JR ELEMENTARY SCHOOL" or "JEFFERSON MIDDLE SCHOOL" so the string reverse method gives you an easy way to work from the right to left by reversing the characters so you are actually working left to right, without having to count characters or string parts.  Just make sure to reverse the text again so you arent showing anybody information that looks like "LOOHCS ELDDIM NOSREFFFEJ"

0 Kudos