Split Name into First and Last
Not too long ago we received a text file of customer names. The problem with the file is it just had one field "Name". One of the requirements of our database application was to have a first name field and a last name field so our customer could search for their customers on either field. As a result we needed to take this text file and split out the data into the two fields from the one.
As we looked into this file, we noticed that the data was in complete disarray! Some of the names were entered like "First Last", and some in the format "Last, First". So, we needed to perform a little string parsing to get the data into our database table. The following is a simplified version of the routine we wrote to attempt to handle each format and create a new Customer object that we could then use to populate our database table.
First we needed to create a Customer class. We created an instance of this Customer class and passed in the "name" value read from the text file into the method of the Customer class named NameSplit().
C#
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void NameSplit(string name)
{
if(name.Length > 0)
{
// Check for a comma
if(name.IndexOf(",") > 0)
{
LastName = name.Substring(0, name.IndexOf(",")).Trim();
FirstName = name.Substring(name.IndexOf(",") + 1).Trim();
}
else if(name.IndexOf(" ") > 0)
{
FirstName = name.Substring(0, name.IndexOf(" ")).Trim();
LastName = name.Substring(name.IndexOf(" ") + 1).Trim();
}
}
}
}
Visual Basic
Public Class Customer
Private mFirstName As String
Private mLastName As String
Public Property FirstName() As String
Get
Return mFirstName
End Get
Set(ByVal Value As String)
mFirstName = Value
End Set
End Property
Public Property LastName() As String
Get
Return mLastName
End Get
Set(ByVal Value As String)
mLastName = Value
End Set
End Property
Public Sub NameSplit(ByVal name As String)
If name.Length > 0 Then
' First check for a comma to see if they entered Last, First
If name.IndexOf(",") > 0 Then
mLastName = name.Substring(0, name.IndexOf(",")).Trim()
mFirstName = name.Substring(name.IndexOf(",") + 1).Trim()
ElseIf name.IndexOf(" ") > 0 Then
mFirstName = name.Substring(0, name.LastIndexOf(" ")).Trim()
mLastName = name.Substring(name.LastIndexOf(" ") + 1).Trim()
End If
End If
End Sub
End Class
To test out this method, use the following code:
C#
private void TestCustomerSplit()
{
Customer cust;
cust = new Customer();
cust.NameSplit("Bruce Jones");
MessageBox.Show(cust.FirstName + " " + cust.LastName);
cust = new Customer();
cust.NameSplit("Jones, Bruce");
MessageBox.Show(cust.FirstName + " " + cust.LastName);
}
Visual Basic
Private Sub TestCustomerSplit()
Dim cust As Customer
cust = New Customer()
cust.NameSplit("Bruce Jones")
MessageBox.Show(cust.FirstName & " " & cust.LastName)
cust = New Customer()
cust.NameSplit("Jones, Bruce")
MessageBox.Show(cust.FirstName & " " & cust.LastName)
End Sub
Another way to accomplish this task would have been to use the Split() method of the String class. You can split a string on a specific character like a space or a comma. You then end up with an array of each name. I will leave that as an exercise for you to do! :)
You just have to love the string parsing capabilities of .NET! Just a little bit of code was all it took to make this job of turning different formats into something that was much easier to work with.
Good Luck With Your Coding,
Paul Sheriff
** SPECIAL OFFER FOR MY BLOG READERS **
Visit http://www.pdsa.com/Event/Blog for a free eBook on "Fundamentals of N-Tier".
Past Blog Content
Blog Archive
-
2015
-
2014 (18)
-
2013 (11)
-
2012 (19)
-
2011 (29)
-
2010 (19)
-
2009 (28)
-
2008 (0)
-
2007 (14)
-
2006 (6)