This is kind of a two-part question to see which route would be more efficient.

I'm trying to display a label in a shortened version, but the info is all in one column:

Township 1 South Range 20 West



I tried substr() expression, but since the numbering (for both numbers in the string) vary from 1-29 I cannot use a long substr() expression that joins them together. It moves the start/stop position.

So, I was thinking to somehow do what MS Excel does with "text to columns", but in QGIS. Is there any way to do that? Or, is there an easier way to do what I'm looking for?

  • 2
    you could use string_to_array("column", ' ') (note the space character as split mark) and go from there (access via common array notation, array[<index>])? – ThingumaBob Jun 18 at 22:19
  • nevermind, with your edit I realize that you want to abbreviate that string. – ThingumaBob Jun 19 at 6:10

Working with @ThingumaBob's string_to_array suggestion:

    with_variable('a',string_to_array('Township 1 South Range 20 West',' '),
        left(@a[5],1) )

You can split the string into an array and then get the first letter of each element and join them together. For the numbers instead of getting the first character you add a zero '0' to the start and the retrieve the two characters on the right. Just replace the hard coded 'Township 1 South Range 20 West' with the name of the column containing the data.

This expression will work in QGIS 3 but not in QGIS 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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