[GIS] Remove first n characters in label using ArcGIS 10 label expression builder with VBscript

labelingvbscript

I have a planning cadastre dataset. It has the type of plan (DP, RP or SP) and then the plan number, thusly:

DP123456
RP1248590000
SP3855
etc...

I want to make a label in the expression builder in ArcGIS 10 which skips the first two characters and only has the plan number. In full, this is what I have done:

left([planlabel],2)&vbnewline&left([planlabel],len([planlabel]-2))&vbnewline& [lotnumber] 

You can see here that I am making a stacked label which will put the type of plan on the first row (it is always the first two characters of [planlabel] and then the second row has the plan number, followed by [lotnumber] on the last line.

In Excel, string indexing of this sort is apparently achieved by CELL,len(CELL-2) and I was hoping the same would hold for VBscript in ArcGIS but apparently this is not the case.

Any clues?

Best Answer

This can be done very simply in Python, by selecting the Python parser and then ticking the advanced box. The code below does what you attempted to do in VBScript, using \n as a newline character and Python's powerful string indexing:

def FindLabel ( [planlabel], [lotnumber]  ):
   return [planlabel][:2] + '\n' + [planlabel][2:] + '\n' + [lotnumber]

To do this in VBScript the code block would be as follows, again ticking the advanced box:

Function FindLabel ( [planlabel], [lotnumber]  )
  FindLabel = left([planlabel],2) & vbnewline & mid([planlabel],3,len([planlabel])) & vbnewline & [lotnumber] 
End Function

This uses the mid function to take the 3 character to the end of the string, defined using the len() function which gives the length of the string. I hope you'll agree the python syntax is a lot cleaner, but both have the same result.

Edit:

For completeness, so that this answer can be used as a reference for people using Javascript, here is the same label function in Javascript, again tick the advanced box:

function FindLabel ( [planlabel], [lotnumber] )
{
  return [planlabel].substring(0,2) + '\n' +[planlabel].substring(2,[planlabel].length) + '\n' +  [lotnumber];
}
Related Question