[GIS] How to truncate the %Name% string for inline variable substitution

iteratormodelbuilder

I'm using model builder to iterate through feature classes in datasets. With each sucessive process step (each model) I'm using the %Name% variable to name new feature classes in a new dataset.

I found that I can't just use %Name% without adding a little to the name of the new feature class; the model didn't seem to like working with two feature classes with the same name. To write the new feature classes to disk, I'm using something like %Name%_AppendedBit. The problem is that with sucessive steps I keep adding more and more bits to the name of the feature class.

I tried using Calculate Value with something like Replace(%Name%,_AppendedBit,_JustIdentifyThisStep) to make a variable with which to name the new feature class, but %output_value% doesn't appear to have any value (just "1"). I tried this as a variant and as a string. I was using vb Replace in the "expression" box, not messing with the "code block" box.

I could use %n% to name the feature classes, but then I lose useful detail from the original feature class name. This could be worked around by doing a spatial join later.

So, the long and the short of it is I'd like to use some kind of string truncation with inline variable substitution.

Best Answer

Slicing should do the trick. To slice a string do this "%var%"[x:y], where x and y are indices representing the start and end positions of the bits of the string you want to keep. The first character of a string has index 0.

say you got:

var = "myfc_clip"

and you want

myfc_buffer

not

myfc_clip_buffer

you would do something like this

"%var%"[0:5] + "buffer"

this will give you

myfc_buffer