Board index » delphi » separate First Name and Last Name

separate First Name and Last Name

Hi There!
I have name and surname in EditBox  how can I separate them and input in
a database or in a two new EditBox.
Thanks!
se...@cg.yu
 

Re:separate First Name and Last Name


Assuming that the surname is last, the two names are separated by at least
one space, and
your Edit Box is called 'Edit1':

var
  First, Last: string;
  Space: integer;
begin
  Space := Pos(' ', Edit1.Text);
  First := Trim(Copy(Edit1.Text, 1, Space));
  Last := Trim(Copy(Edit1.Text, Space, Length(Edit1.Text)));

Do with them what you will, once they're separated.

Ray

Quote
"Senad Skrijelj" <se...@cg.yu> wrote in message

news:1019890704.984645@tvrdi.cg.yu...
Quote
> Hi There!
> I have name and surname in EditBox  how can I separate them and input in
> a database or in a two new EditBox.
> Thanks!
> se...@cg.yu

Re:separate First Name and Last Name


Splitting up in words can also be done using the 'nthword' function from the
psock unit. Try help and you'll find everythng you need.
Splitting up names is extremely tricky, and never 100% error proof, mainly
because of middlenames, titles etc. etc.
Why not input them separately in 2 or 3 inputboxes

Roel

Quote
"Ray Cernis" <cern...@westonedesign.com> wrote in message

news:3c7cb86c_1@news2.prserv.net...
Quote
> Assuming that the surname is last, the two names are separated by at least
> one space, and
> your Edit Box is called 'Edit1':

> var
>   First, Last: string;
>   Space: integer;
> begin
>   Space := Pos(' ', Edit1.Text);
>   First := Trim(Copy(Edit1.Text, 1, Space));
>   Last := Trim(Copy(Edit1.Text, Space, Length(Edit1.Text)));

> Do with them what you will, once they're separated.

> Ray

> "Senad Skrijelj" <se...@cg.yu> wrote in message
> news:1019890704.984645@tvrdi.cg.yu...
> > Hi There!
> > I have name and surname in EditBox  how can I separate them and input in
> > a database or in a two new EditBox.
> > Thanks!
> > se...@cg.yu

Re:separate First Name and Last Name


If one may have to cope with multiple given names and/or initials I'd
suggest

procedure SplitName (const aName : string; var Surname, GivenNames :
string);

var i : integer;

begin
i := Length (aName);
while (i > 0) and (aName [i] <> ' ') do
    dec (i);
GivenNames := Trim (Copy (aName, 1, i));
Surname := Trim (Copy (aName, i, MaxInt));
end;

Of course parsing surnames is not quite so simple, especially in some
languages. Consider surnames such as "St. Simmon", "van Eclker", etc. Unless
one has a data set where one knows how many given names (and/or initials)
there are in each instance, the problem is not easily solvable for all
cases.

Other Threads