How to remove spaces with Calculate field in ArcMap?

22873
10
04-21-2016 12:43 PM
LianaSmith
Occasional Contributor II

My values are padded with spaces. Sometimes it's "1", sometimes it's " 1" sometimes its "1  ".

I need to remove all the spaces before, between, after, wherever they appear...

Thanks!!!!

0 Kudos
10 Replies
ChrisSmith7
Frequent Contributor

For leading and trailing whitespace, I would recommend using Python String strip() Method

Here's an example:

For any and all spaces, try replace:

LianaSmith
Occasional Contributor II

Thanks, Chris. I need to use it in the Model. I am not familiar how I can add this to the model... Is there any way to do it with Field Calculator?

0 Kudos
ChrisSmith7
Frequent Contributor

Sure, here's an example in the model (for replace):

This tool is available under...

>> Data Management Tools

>> Fields

>> Calculate Field

0 Kudos
LianaSmith
Occasional Contributor II

Chris, it works but I get zeros for some of the values..

For example, before I had "B", "  B", "000B2"

After calculation I get: "B", "000B", "000B2"

I don't want it to be "000B"

For the rest of the records it worked fine

0 Kudos
ChrisSmith7
Frequent Contributor

So you want to remove whitespace AND zeros? If you'd like to remove zeros as well, is it just leading zeros?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Chris... I would wait until you get a list of all possible iterations, permutations and combinations that could be in the table.  This sounds like a test of the most horrible kind... working in modelbuilder...with an ever increasing list of new conditions.

0 Kudos
ChrisSmith7
Frequent Contributor

I was thinking along those lines since it sounds like scope is creeping a bit...

Liana,

Just a warning, too - make sure you have a back-up of your data before running processes that are final!

LianaSmith
Occasional Contributor II

I want to remove just the spaces. Some of the values should have zeros before them. The problem is that after calculation for the case "   B" (three spaces before letter) I get "000B". It should become B, not 000B

0 Kudos
ChrisSmith7
Frequent Contributor

I see now... that's strange - can you give your processes step-by-step? Are you positive "000B" wasn't the value before the strip/replace was executed? Could you possibly supply a file, without any identifiable info, exemplifying the issue?

0 Kudos