Easy question working with strings

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

I have a Name field where I need to extract just the person's last name. I
did not originally set up the database as I would have separate fields for
first and last name, but this data is being pulled from an AS400 where it is
just one field.

I was thinking of something like trying to find the space in the name,
marking that location and then trimming the beginning of the string, but I
would have to start searching for the last space because the middle name
could be included.

So I have: Douglas Bell
Nancy E Basham

I want to extract just 'Bell' and 'Basham'.

Is there an easy way to go about this?

Thanks for the advice.

Brad
 
Brad,

Maybe when you live in the US this will be possible, however when you use
names from other countries you probably can forget it. (Surely when you have
Dutch names).

Cor
 
Dim X As String
X = "A TEST STRING"
MessageBox.Show(X.Substring(X.LastIndexOf(" ")))

Note: Test the value returned from LastIndexOf before using it. If there
is no " ", substring will throw an exception.

Chris
 
I don't believe this will throw an exception if no " " found...

Dim s As String = "A TEST STRING"
Dim i As Integer = Microsoft.VisualBasic.InStrRev(s, " ")
s = Microsoft.VisualBasic.Mid(s, i + 1)

Greg
 
Thanks for the advice as it helped greatly.

Actually though, because this is a field that I need in Crystal Reports
right now I used this:

dim lastName as string
lastName =
Right({Entrants.Owner1},(Length({Entrants.Owner1})-(Instr({Entrants.Owner1},chr(32)))))
formula = lastName

But, I am thinking of adding a field to the acutal database and when the
field is saved then splitting out the last name and saving it to that field.

Thanks again,

Brad
 
You might use string.split using " " as a delimiter.
Then, look for the value in the last element of the returned string array.
 
Back
Top