Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting last word from string, or trying to...

Status
Not open for further replies.

ukleaf

Programmer
Mar 16, 2004
27
GB
Hi all,

I am using the following code to try and extract the second word of a string.

Code:
global stringvar secondName := {user.us_fullname};

If InStr(secondName, " ") > 0 Then secondName := Right(secondName, InStr(secondName, " "));

Ok, so for example the full name is Joe Bloggs the result of the above code is "e Bloggs" instead of just "Bloggs"

What am I doing wrong? I'm using the same code to extract the first word of the string and that's working fine, see below.

Code:
global stringvar firstName := {user.us_fullname};

If InStr(firstName, " ") > 0 Then firstName := Left(firstName, InStr(firstName, " "));

This always results in showing what I need, "Joe"

Any help would be great, many thanks.
 
Hi,
Ok, lets see how each function will resolve:
Assume : {name} = 'Joe Smith'
Instr({name},' ') will return 4
But,
Right({name},4) or Right({name},Instr({name},' ')
should actually return 'mith'
since Right( from the docs)
Extracts the given number of text characters from the right side of the specified string.

so it should extract the last 4 characters from the right end.
Instead use:
{name}[Instr({name},' ') + 1 to Length({name})]

That will return Smith.

Other methods may also work, but I use this one a lot..

[profile]





 
Keep in mind that fullnames aren't limited to 2 parts, a last name alone can have several.

The following will allow for multiple names, by taking the last name in the field and placing it first followed by a comma:

stringvar array Name:=split({Customer.Customer Name}," ");
stringvar LastName:=split({Customer.Customer Name}," ")[ubound(split({Customer.Customer Name}," "))];
if instr({Customer.Customer Name}," ") > 0 then
redim preserve Name[ubound(split({Customer.Customer Name}," "))-1]
else
redim preserve Name[ubound(split({Customer.Customer Name}," "))];;
LastName & ", " &join(Name," ")

-k
 
Thanks again synapsevampire, this works a treat also.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top