Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a bunch of point data that needs labeling. However the attribute used for labeling is more complex than is needed in the map.

Example of the attribute field for labeling:

  • 15/8-9a,
  • 001/15-2
  • 8/19-13

What we need to see labeled is any number and/or letter found after the "-" symbol. So from the above example: 9a, 2, 13 will be displayed on the map.

I know this can be done with slicing but since the number of characters after "-" is variable I'm struggling a little with the expression.

I do not want to add a new field so am exploring writing a label expression.

Does anyone know what the expression might look like to handle this?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

I would use a Python expression like the following by checking Advanced on the Label Expression dialog of the Labels tab of the Properties dialog:

def FindLabel ( [TestField] ):
  return [TestField].split("-")[1]

This relies on there only being one occurrence of "-" in the field.

share|improve this answer
1  
Cheers everyone...this will make a good reference page as well as those displayed by Get Spatial. I like how short this expression is! –  spk578 Aug 19 at 10:44

Try:

def FindLabel ( [name_field] ):
 a=[name_field]
 l=a.rfind("-")+1
 a=a[l:]
 return a
share|improve this answer

First, the resource page to help you in the future: Building label expressions

There are a lot of examples on that page, including Python, VBScript and JScript.

If it were me, building the expression in VBScript, I would use a combination of the Instr, Len, and Mid functions. If it is supported, use the InStrRev function, as that would let you search for the first instance of a "-" starting from the right.

You want to find the position of the "-" in the string, then count from there to the end of the string.

The definition of the Mid function is (Input String, Start Position, Number of Characters)

Mid([string],Instrrev([string],'-')+1,len([string])-instrrev([string],'-')

You just build your label function around this. Adding 1 to the Instrrev moves you to the character beyond the "-".

The bottom of that help document has links to string references for Python and VBScript. They are both exceedingly helpful for remembering these sorts of operations. I would look at Python as it can probably accomplish this same activity with a much smaller function length.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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