Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Extract Parts of a String Post Reply Post New Topic
Author Message
ml05
Newbie
Newbie
Avatar

Joined: 20 Oct 2011
Online Status: Offline
Posts: 2
Quote ml05 Replybullet Topic: Extract Parts of a String
     Posted: 06 Dec 2011 at 2:52pm
Hi, 
I have a text field that contains customer names like this:
The field contains the title, first name, surname and suffixes in one field.
 
Example: Mrs. Sheila Customer1
Example: Mr. & Mrs. Mike Customer2 Sr.
Example: Mr. & Mrs. Mark Jones3
 
I need a formula that will extract the title and surname only like this:
Example: Mrs. Customer1
Example: Mr. & Mrs. Customer2 Sr.
Example: Mr. & Mrs. Jones3

I tried using split function and several mid strings formula but they don't account for the spaces and suffixes in the last names.

Can you assist?  Thank you,
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 07 Dec 2011 at 5:01am
this is a tough one, as everything is variable and I don't know the rules.
is there always a title, first name, surname?
is it always mr. & mrs.  ie never mrs. & mr. , but I would assume that Mr. & Dr. is valid so let's see what we can come up with.
 
local numbervar lst := 0;
local numbervar pos;
local stringvar f := {table.field};
local stringvar titles := "";
 
pos := instr(f, "Mr.");
if pos > lst then lst := pos;
pos := instr(f, "Mrs.");
if pos > lst then lst := pos;
pos := instr(f, "Dr.");
if pos > lst then lst := pos;
//etc for as many titles as you recognize
 
//now find the last period of the titles
pos := instr(lst, f, ".");
f := trim(mid(f, pos + 1));  //remove all the titles
titles := left(f, pos);           //save the titles for later
 
pos := instr(f, " '");   //find the space after the first name
titles + " " + mid(f, pos + 1)
 
 
if there are middle initials, this won't work, but hopefully it is at least a path to a solution.
 
HTH
IP IP Logged
ml05
Newbie
Newbie
Avatar

Joined: 20 Oct 2011
Online Status: Offline
Posts: 2
Quote ml05 Replybullet Posted: 07 Dec 2011 at 9:26am
Thanks. There is always a title, first name and surname.
The titles will always be
Dr.
Dr. & Mrs.
Miss
Mr.
Mr.& Mrs.
Mrs.
Ms.

Right now the code is showing the entire name without the titles. Is there a way to save the surname in a variable to always appear? My suggested workplan would be to save save the titles and extract the last name and then combine them in a string.
Here is the code now:
local numbervar lst := 0;
local numbervar pos;
local stringvar greeting := {table.field};
local stringvar titles := "";

//titles
pos := instr(greeting, "Dr.");
if pos > lst then lst := pos;

pos := instr(greeting, "Dr. & Mrs.");
if pos > lst then lst := pos;

pos := instr(greeting, "Miss");
if pos > lst then lst := pos;

pos := instr(greeting, "Mr.");
if pos > lst then lst := pos;

pos := instr(greeting, "Mr.& Mrs.");
if pos > lst then lst := pos;

pos := instr(greeting, "Mrs.");
if pos > lst then lst := pos;

pos := instr(greeting, "Ms.");
if pos > lst then lst := pos;

//now find the last period of the titles
pos := instr(lst, greeting, ".");
greeting := trim(mid(greeting, pos + 1)); //remove all the titles
titles := right(greeting, pos);           //save the titles for later

pos := instr(greeting, " ");   //find the space after the first name
titles + " " + mid(greeting, pos + 1)
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 07 Dec 2011 at 9:39am
do you want to keep things like Jr. Sr. III?
again, it's not unheard of to have Mr. & Dr. (the wife has title)
or Dr. & Dr. (they both have phDs)
I don't know of all the combinations, but there would seem to be more than you have allowed, but the biggest the problem that will arise is that there MISS does not have period, so the first name will not be removed. This would imply some more logic specific to 'Miss '.
 
well, the last line should be combining the title string with the surname.  If just the title is being removed, this would seem to imply that 1) the titles are not being selected correctly. With that in mind, I would reverse the titles:= & greeting:= as this is probably the root of the issue, as you telling the report that the greeting includes the title, but it doesn't as you deleted in the line above...oops my bad, I should have reversed them. 
 
drat, i'm not perfect ;)
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum