Using the LTRIM and RTRIM functions to eat up more than 1 blank space

10532
11
Jump to solution
03-22-2016 01:54 PM
ScottLouque
New Contributor III

I have 3 fields which I am concatenating together into a LABEL field

Pre_DIR, STR_NAME and SUFFIX

      X          XXXXX                XX

I am trying to use the LTRIM and RTRIM functions to eat up the blank spaces so the column lines up properly

My expression in the field calculator looks like this

RTRIM( [PRE_DIR]) & " " & LTRIM([STR_NAME]) & " " & [SUF_FIX]

but my column still looks like this

X  XXXXX XX

XXXXX XX

X  XXXXX XX

What is wrong with my function? Could someone point out to me Why is it not lining up along the left side of the column?

Thanks,

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

I think the string functions are built into the field calculator, so you can ignore the import string line and probably the prefix to the strip functions.  But in any event, time to move on to python, so make sure you set the parser to python.

>>> import string
>>> a
'  '
>>> b
' street name '
>>> c
' suffix '
>>> abc = "{} {} {}".format(a,b,c)
>>> abc
'    street name  suffix '
>>> string.strip(abc)
'street name  suffix'

so in the field calculator your expression would be

string.strip(a + b + c)
'street name  suffix'

but using field names which in python are enclosed in ! marks

string.strip( !a! + !b! + !c! )
'street name  suffix'

View solution in original post

11 Replies
ScottLouque
New Contributor III

That didn't post right.

What I am trying to do is eat up 2 blank spaces to get rid of the space for those streets without a 'Direction' in the name to make them line up along the left side of the column

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I think the string functions are built into the field calculator, so you can ignore the import string line and probably the prefix to the strip functions.  But in any event, time to move on to python, so make sure you set the parser to python.

>>> import string
>>> a
'  '
>>> b
' street name '
>>> c
' suffix '
>>> abc = "{} {} {}".format(a,b,c)
>>> abc
'    street name  suffix '
>>> string.strip(abc)
'street name  suffix'

so in the field calculator your expression would be

string.strip(a + b + c)
'street name  suffix'

but using field names which in python are enclosed in ! marks

string.strip( !a! + !b! + !c! )
'street name  suffix'
curtvprice
MVP Esteemed Contributor

Hey Dan - I am pretty sure the string methods are now attached to all strings (I think this happened at Python 2.2 or something), and string literals (which is how Python sees string fields in the Field calculator) can be appended just be listing them one after the other.

This syntax is sparse, but is beautiful, and very easy to read. LOVE PYTHON.

(!a! " " !b! " " !c!).strip() # Calculate Field
( " "  " " ).strip() # Python label expresson

As Ted pointed out, this will generate an error if one of the field values are null (you can't add a string to a null) or numeric (same deal). So in that things are more complex, but still so pythonesque, using a list comprehension. Dan Patterson​ you gotta love this:

" ".join([str(s).strip() for s in [!a!, !b!, !c!] if s])

but what if the field has a numeric value of zero? Won't that evaluate to False and leave it out of the string? We can fix that!

" ".join([str(s).strip() for s in [!a!, !b!, !c!] if (s or s == 0)])
0 Kudos
TedKowal
Regular Contributor II

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

ahhhh well, that is easy,

>>> nulls = [["not null", "Null"][i in ["",[],{},None]] for i in ["", 0, " ",[],[1],{},None] ]
>>> nulls
['Null', 'not null', 'not null', 'Null', 'not null', 'Null', 'Null']
>>>

But of course, no one would be foolish to use 0 to represent nodata in any event

0 Kudos
TedKowal
Regular Contributor II

Since you are using VBScript:

RTRIM( [PRE_DIR]) & " " & LTRIM([STR_NAME]) & " " & [SUF_FIX]  <--- If the Pre_Dir is blank then you will still have  two blanks befor the Str_Name... 

You may want to do something like this using the Advance code section for your label

if trim([pre_dir] & "") = ""  'Handles Null cases as well as blanks
   myLabel = ltrim(rtrim([str_name] & "" [suf_fix]))
else
   myLabel = RTRIM( [PRE_DIR]) & " " & LTRIM([STR_NAME]) & " " & [SUF_FIX]
end if

** Had to add in the old fashion VB Null test trick to put a little mud in the eyes of the Python Lovers!  Python handles null values horribly!  I have had issues with python strings stripping when true null values when reporting from databases... even when I use python I find myself concatenating a blank purposely when a field could be null then stripping the blank away.

ScottLouque
New Contributor III

Thanks Teb!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I have more examples... Python handles them quite well

>>> nulls = [["not null", "Null"][not i] for i in [""," ",[],[1],{},None] ]
>>> nulls
['Null', 'not null', 'Null', 'not null', 'Null', 'Null']
>>>
0 Kudos
TedKowal
Regular Contributor II

uggg give headache reading the logic....  guess it is better than

xstr = lambda s: s or ""

print xstr(myString)

a() = ("not null","Null")

if not isNull(a) then a="Cannot be Null"

(Yes I know only in vb.net not vbscript this true)

0 Kudos