Label class expression to strip words

284
1
08-05-2019 01:28 PM
LS1
by
New Contributor III

Novice in python. Have tried several expressions but can't seem to strip the words 'Branch' and/or 'Library' from a field of names...some names are "[name of city] Branch Library" and others are "[name of city] Library". I don't want my labels to show either of these on the map because it looks too busy. Some of the city names contain the string "Ranch" so those were also getting stripped - for example if the name is "Weston Ranch Branch Library" I only want the label to be "Weston Ranch" or if the name was "Weston Library" only display "Weston."  Thanks.

0 Kudos
1 Reply
LanceCole
MVP Regular Contributor

Lisa, 

One quick way using python is the following code.  You can customize the list of words to be striped from your string.  Please do note, this particular rendition is case-sensitive so 'Branch' is not he same as 'branch' and the latter would not be removed.  

string = "Weston Ranch Branch Library"
stripList = ['Library','Branch']
print ' '.join(i for i in string.split() if i not in stripList)‍‍‍‍‍‍

In a label expression it would be along the lines of the following and you would replace [string] with the name of your attribute: