Last Name First

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I know I'm reinventing the wheel here, and not very elegantly. Can somebody show me a less embarrassing way of coding this

Dim s1 As String = "Dr. John Smith".Tri
Dim s2 As String = s

txtFirstString.Text = s
Dim i1 As Integer = s1.LastIndexOf(" "
s1 = s1.Substring(i1 + 1, s1.Length - i1 - 1
s1 = s1 & ",
s1 = s1 & s2.Substring(0, i1
txtSecondString.Text = s
txtSecondString.Text = Smith, Dr. Joh

thanks

polynomial5d
 
Your code seemsa moderately confusing because you're intermixing s1 and s2.
(You're first creating s1 as the source, then assigning a copy to s2, and
then modifying s1.) It might be more readable as:

Dim source As String = "Dr. John Smith".Trim
Dim result As String

Dim i As Integer = source.LastIndexOf(" ")
result = source.Substring(i + 1, source.Length - i - 1)
result &= ", "
result &= source.Substring(0, i)

You could also define "result" as a StringBuilder instead of a string, and
use Append to add the three different segments. OTOH, your way isn't bad --
there really isn't a simpler way to do it.


I know I'm reinventing the wheel here, and not very elegantly. Can
somebody show me a less embarrassing way of coding this?
 
You might fínd String.Split useful...

I know I'm reinventing the wheel here, and not very elegantly. Can
somebody show me a less embarrassing way of coding this?
 
Back
Top