[GIS] Splitting (stacking) labels in ArcMap in multiple lines using vbScript

arcgis-desktoplabelingvbscript

I want to show labels in ArcMap on multiple lines. The label I have is written as: Caritas Lebanon Migrant Center, DRC, Handicap International, NRC, SCI.
I want to show it in separate lines. e.g.:

Caritas Lebanon Migrant Center, DRC, 
Handicap International, NRC, SCI

Or:

Caritas Lebanon Migrant Center, DRC, 
Handicap International, NRC, 
SCI

Is that possible to do using an expression?

Best Answer

You could probably write a vb expression to replace every second comma, but I find it easier to work in python. Something like this would work:

def FindLabel([LABELFIELD]):
    labels = map(str.strip, [LABELFIELD].split(','))
    out = ''
    for i in xrange(0, len(labels), 2):
        if i + 1 < len(labels):
            out += '{}, {}\n'.format(labels[i], labels[i + 1]))
        else:
            out += labels[i]
    return out