4

I have a field name where all the values are capitalized so I used title('string') to get my desired results:

 'OLONGAPO CITY'

will result to

 'Olongapo City'.

However, there are strings such as:

 CITY OF MAKATI

I want it to appear as

'City of Makati' 

instead of

'City Of Makati' 
6

You could try using something like the following expression:

replace(title("fieldName"), 'Of', 'of')
  • 1
    Great that was quick! – brentiemapper Jun 27 '17 at 11:21
  • @brentiemapper - Most welcome, glad it was helpful :) – Joseph Jun 27 '17 at 11:23
1

If you want proper title case rules (including correct handling of a/of/the/etc), you could grab the source of Titlecase.py (See https://muffinresearch.co.uk/titlecasepy-titlecase-in-python/ ) and insert this in a custom expression function from the "Function Editor" tab in the expression builder:

enter image description here

The code look something like:

@qgsfunction(args='auto', group='String')
def title_case(value1, feature, parent):
    return titlecase(value1)

...followed by the entire copy/pasted contents of https://raw.githubusercontent.com/ppannuto/python-titlecase/master/titlecase/__init__.py

You'll then be able to use this new title_case function anywhere in QGIS expressions: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.