1

I am looking to get the first letter and second letter (after white space) in the QGIS variable @user_full_name to provide me with the initials of the QGIS user that has created a project.

For instance, James Lewis > JL

My understanding is that the regexp_substr function can be used to achieve this, however I am not sure how the syntax should look to access the first character after the second white space.

3 Answers 3

1

Usually the project author is located under the @project_author, however, you can use the following expression to extract the initials of the project author, and it should be applied without a problem if you replace @project_author with @user_full_name. But in this example, I am using @project_author. Please make sure to adjust the start and length of the substr(string,start[,length]) based on string that you have.

@project_author || ': ' || '(' || substr(@project_author, 1,1) ||  substr(@project_author, 7,1) || ')'

Here is the input in expression:

enter image description here

Here is the output:

enter image description here

Update

If you want the initials to be created automatically, you need to create a custom function for it. You can use the following function to create the initials:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def nameInitial(text_field, feature, parent):
    split_text = text_field.split()
    initial = [nm[0] for nm in split_text]
    joinNM = "".join(initial)
    return joinNM

enter image description here

Then from Custom functions select the function the you created (nameInitial in this example) and @project_author.

enter image description here

The output is same as the above but without user adjustment:

@project_author || ': ' || '(' || nameInitial(@project_author ) || ')'

enter image description here

4
  • Thanks Ahmad. The idea here was to have the expression such that a user would not have to manually tweak the expression.
    – R.B
    Jun 24, 2019 at 9:30
  • @RyanBell I updated the answer to avoid users adjustments.
    – ahmadhanb
    Jun 24, 2019 at 9:58
  • Thank you so much, this works perfectly. The only adjustment I have made is changing project_author to user_full_name as this fits my use case better.
    – R.B
    Jun 24, 2019 at 12:03
  • @RyanBell Glad it works. Please accept the answer so that it can help others.
    – ahmadhanb
    Jun 24, 2019 at 12:05
0

Try:

left(left( "Name", strpos( "Name" ,' ')),1) || left(replace(right( "Name", strpos( "Name" ,' ')),' ',''),1)

this expression only works correct if you have only one whitespace.

7
  • Thanks for the response. Using the example name James Lewis the output I get is '[L'
    – R.B
    May 30, 2019 at 9:25
  • @RyanBell like I said, this only works fine if you only have one whitespace in your string. It will turn the string James Lewis to JL. The first part extracts the first letter of your string left to your whitespace. The second part extracts the first letter of your string right to your whitespace. Because this is the whitespace itself it will be replaced with nothing and the 'second' character is taken instead. Tested in QGIS 2.18.18 and QGIS 3.6.0
    – MrXsquared
    May 30, 2019 at 14:11
  • I am using QGIS 3.4.8 with the example James Lewis but for some reason it does not work. The output changes to "[Jl" with: 'left(left( "James Lewis", strpos( "James Lewis" ,' ')),2) || left(replace(right( "James Lewis", strpos( "James Lewis" ,' ')),' ',''),1),' but there is only one white space?
    – R.B
    May 30, 2019 at 14:49
  • 1
    @RyanBell try: left(left( 'James Lewis', strpos( 'James Lewis' ,' ')),1) || left(replace(right( 'James Lewis', strpos( 'James Lewis' ,' ')),' ',''),1)
    – MrXsquared
    May 30, 2019 at 14:53
  • Problem solved, thank you I appreciate your help with this. For some reason this does not work for all users. My colleague only has one white space in his name but this does not seem to pull the initials correctly. I am using the variable @user_full_name to achieve this. Any thoughts?
    – R.B
    May 30, 2019 at 14:56
0

You can divide the name in list and join the first char :

name = "James Lewis"
initials= ''.join([word[0] for word in name.split()])
print(initials)
2
  • Thank you, but I am not sure you can use the split function or print in the QGIS expression builder.
    – R.B
    May 30, 2019 at 9:26
  • Your problem isn't well describe. I guess you want the creator of qgis project through a python script. Not in query expression builder. Native Query expression builder is useful for querying layer, not for qgis environnement or project. You can test my answer without print function and replacing name with qgis variable!
    – Sociabilis
    May 30, 2019 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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