how to find name within a name

navisionerinnavisionerin Member Posts: 161
Hello,

newbie here

I have this name 'Name Name2 Name3 Surname'. From this I need to extract the surname.
How is this possible. If they were two names only, I could exclude up to the first space but what if they are more?

Best Answers

  • txerifftxeriff Member Posts: 480
    Answer ✓
    can you provide an example?

    if the string has the format you have defined (with spaces between each word) then you can start looping from last char until you find a blank space, char by char
  • ShaiHuludShaiHulud Member Posts: 186
    Answer ✓
    FullName := 'Name Name2 Nam3 Surname';
    if StrPos(FullName, ' ') <> 0 then
      LastName := SelectStr(StrLen(DelChr(FullName, DelChr(FullName, '=', ' '))) + 1, ConvertStr(FullName, ' ', ','));
    else
      LastName := FullName;
    
    SelectStr returns the specified element from a comma separated string.
    StrLen(DelChr(FullName, DelChr(FullName, '=', ' ')))
    
    returns the number of spaces in the name
    And +1 gives you the number of names, because there's always more names than spaces
    ConvertStr(FullName, ' ', ',')
    
    replaces spaces in the name with commas for SelectStr to work
    And we need to add a check that there really are spaces in the name (maybe incorrect data entry, or a case of mononym)

Answers

  • txerifftxeriff Member Posts: 480
    Answer ✓
    can you provide an example?

    if the string has the format you have defined (with spaces between each word) then you can start looping from last char until you find a blank space, char by char
  • navisionerinnavisionerin Member Posts: 161
    the format defined, yes. COuld you point me how to do this? would it be
    repeat until?
  • ShaiHuludShaiHulud Member Posts: 186
    Answer ✓
    FullName := 'Name Name2 Nam3 Surname';
    if StrPos(FullName, ' ') <> 0 then
      LastName := SelectStr(StrLen(DelChr(FullName, DelChr(FullName, '=', ' '))) + 1, ConvertStr(FullName, ' ', ','));
    else
      LastName := FullName;
    
    SelectStr returns the specified element from a comma separated string.
    StrLen(DelChr(FullName, DelChr(FullName, '=', ' ')))
    
    returns the number of spaces in the name
    And +1 gives you the number of names, because there's always more names than spaces
    ConvertStr(FullName, ' ', ',')
    
    replaces spaces in the name with commas for SelectStr to work
    And we need to add a check that there really are spaces in the name (maybe incorrect data entry, or a case of mononym)
  • navisionerinnavisionerin Member Posts: 161
    thank you!!!!! @ShaiHulud
  • txerifftxeriff Member Posts: 480
    edited 2022-01-13
    I would use a loop and start from the ending, something like (untested):
    mystring:='Name Name2 Nam3 Surname';
    vFound:=false;
    vStrlen:=strlen(mystring);
    vSurname:='';
    vCharCount:=0;
    repeat
      vCharCount+=1;
      if copystr(mystring,vStrlen,1) =' ' then //here you may need to check if its a blank space, maybe convert it     
       vFound:=true 
      else     
        vstrlen-=1;
    until (vFound) or (vstrlen<=0)
    
    //now you know where last blank space starts copy  from there.
    //also vcharcount could be the difference between strlen(mystring)-vStrlen
    vSurname:=copystr(mystring,vStrlen+1,vCharCount);
    message(vsurname);
    

    this ensures you always get the last word.
  • navisionerinnavisionerin Member Posts: 161
    thank you @txeriff
Sign In or Register to comment.