I am using QGIS 2.18.

I have a point shapefile showing ranger stations in a park. The attached image is the field I want to use to label my points. I want to omit "Ranger Station" from showing in the label on the map.

enter image description here


You can use the the strpos function for this if the name Ranger Stations is always constant.

substr(  "LOC_NAME" , 0, strpos( "LOC_NAME", 'Ranger Station'))

The function strpos prints out the string between the given start and endposition. Startposition is easy and set to 0. The end position is delivered by the function strpos. This function delivers the first occurence of the search string. Thats your endposition for the substr function.

  • Thanks @Matte! I tried your suggestion as well and it worked great. – user94618 Apr 18 '17 at 1:48

In your Layer Properties dialogue box, fill in the Label with box with the name of your column (LOC_NAME) (osm_type in my example), and click the Sigma button to the right (circled in red in the image below). Layers Property dialogue

In the new dialogue box that opens, you can write a string query to get rid of your unwanted text. Your query will probably be replace(LOC_NAME, ' Ranger Station', '')

The first argument is the input string, the second is the text that you want replaced, and the third is what you want it replaced with. In your example, you want the unwanted text replaced with nothing, so you put in two single quotes with nothing between them. (Note also that you probably want to get rid of the space that is currently between the text you want and "Ranger Station," so I included an extra space within the single quotes of the second argument: ' Ranger Station'.

In the example below, the input string is "relation", and I'm replacing "tion" with "x" to make "relax". Expression builder

  • Thanks @Abijah! Exactly what I was looking for. I can think of several other places I can use this. – user94618 Apr 18 '17 at 1:48

@Matte has a better solution than the one that I am going to post here, but I will post it anyway.

I used a substr & length expression instead.

I am labeling my map with the name field, which has the word "Permit" at the end of each valid field.

enter image description here

I do not want the word "Permit" included in my labels, so in the expression builder of the Labeling dialog, I entered this.

enter image description here

This gets the length of the name field, and I simply counted the the characters I wanted to subtract from the name field for labeling.

Here is the result.

enter image description here

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.