My too long labels take up too much space in regard to the small counties polygons. I want to remove first two and the last two characters from each single label. Currently the labels on the map look like this: PL1607000 PL1816000 LV801200 LT029900 EE700000 etc.

Because in each single label the first two characters are two-letter country code, and the last two characters always two zeros, to make map more clean I want my labels like this: 16070 18160 8012 0299 7000 etc.

I use Maplex Label Engine. My label field is CTYCode, What expression should I type in Label expression dialog box? Is it possible to also make stacked labels from this labels? I want to the first two characters were at the top, and the rest at the bottom. Like this:

16       18     80    02   70
070    160    12    99   00   etc.

How do I write two different expressions. With and without stacking labels, I do not know about programming at all and I would not know how to change the expression.

  • I corrected text and i think its ok now – GregBrown1993 Feb 7 '18 at 14:46

This can be done using the python parser and a simple bit of string slicing.

In the label expression dialog ensure Advanced is ticked on and the parser is set to python as shown below:

Label expression

The code in the expression box is this:

def FindLabel ( [CTYCode] ):
  label = [CTYCode]
  strippedtext = label[2:-2]    # Slice off the first and last 2 characters
  start = strippedtext[:2]      # Extract or "slice" off first 2 characters
  end = strippedtext[2:]        # Extract all characters after the second
  newlabel = start + "\n" + end # Build new label inserting special newline character
  return newlabel

If you don't want them on separate lines simple exclude the new line character where you build the newlabel.

Not the answer you're looking for? Browse other questions tagged or ask your own question.