how to find name within a name

navisionerin
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?
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?
0
Best Answers
-
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 char0 -
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 spacesConvertStr(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)0
Answers
-
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 char0 -
the format defined, yes. COuld you point me how to do this? would it be
repeat until?0 -
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 spacesConvertStr(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)0 -
thank you!!!!! @ShaiHulud0
-
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.
0 -
thank you @txeriff1
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions