Share via


Extract First Name, Middle Name and Last Name from Full Name string

Question

Wednesday, April 16, 2014 11:18 AM

Hi Team,

I would like to write a code which can extract first name, Middle name and last name from a full name string.

Each name (first, middle and last) can accomodate upto 30 char only.

Example :  If full name is  :  -  RAMSHEENA AMBALATHVEETIL SHAHUL HAMEED MAMUTTY

As the 30 characters limit complete on char “U” in word SHAHUL, so first two word “RAMSHEENA AMBALATHVEETIL” should considered as FIRST NAME, and MIDDLE NAME start from SHAHUL to next 30 char, as

SHAHUL + HAMEED + MAMUTTY is less than 30 char including space so MIDDLE NAME should be “SHAHUL HAMEED MAMUTTY”. And in this case there will be no LAST NAME. Otherwise after the completion of MIDDLE NAME rest of the words will fall in LAST NAME.

Thanks,

Isteyak Ahmad

All replies (6)

Wednesday, April 16, 2014 11:37 AM ✅Answered | 1 vote

Private Sub ExtractFirstMiddleLastNames(name As String)
    Dim names As String() = name.Split(" "C)

    Dim firstNameBuilder As New StringBuilder()
    Dim middleNameBuilder As New StringBuilder()
    Dim lastNameBuilder As New StringBuilder()
    For Each n As String In names
        If firstNameBuilder.Length + n.Length <= 30 Then
            firstNameBuilder.Append(n)
            firstNameBuilder.Append(" ")
        ElseIf middleNameBuilder.Length + n.Length <= 30 Then
            middleNameBuilder.Append(n)
            middleNameBuilder.Append(" ")
        Else
            lastNameBuilder.Append(n)
            lastNameBuilder.Append(" ")
        End If
    Next
    Debug.WriteLine("firstname:" & firstNameBuilder.ToString())
    Debug.WriteLine("middlename:" & middleNameBuilder.ToString())
    Debug.WriteLine("lastname:" & lastNameBuilder.ToString())
End Sub

Mark this as answer if you found helful :)


Wednesday, April 16, 2014 11:27 AM | 1 vote

Hello,

Look at using various methods in the String class like Split(" "c) to split tokens in the desired string into a string array and Count for determining the strings length. From there work with logic IF statements to determine what to work with in regards to the 30 char limit.

If input is coming from a TextBox then you can limit the input via MaxLength property.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Wednesday, April 16, 2014 11:40 AM

The input is not coming from text box, it is coming from excel sheet.

and there could be many records (Full name) in excel of various length.

So if the full name is less then 30 char then all come as First Name and if it is more then 30 then need to break in first , middle and last name.


Wednesday, April 16, 2014 12:35 PM

Hello Eswar Duvvu,

If you are posting code please post using VB.NET as this forum is not for C# but VB.NET.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Wednesday, April 23, 2014 5:58 AM

Hello Eswar Duvvu,

If you are posting code please post using VB.NET as this forum is not for C# but VB.NET.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

Thanks,

I have edited my post with VB.Net version of my code. 


Wednesday, April 23, 2014 6:05 AM

No matter from whatever the source you are getting the full name string.

Pass it to the function i have given above it will be splitted into firstname, middlename, lastname as per your requirements in the question.

I have put Debug.Writeline just to see the output.

You can have a structre like below and make the function return it after assigning the values to it.

Class Name
   Public FirstName As String
   Public LastName as String
   Public MiddleName as String

   Public Sub New()
      FirstName = String.Empty
      MiddleName = String.Empty
      LastName = String.Empty
   End Sub
End Class
Private Function ExtractFirstMiddleLastNames(name As String) as Name
    Dim names As String() = name.Split(" "C)

    Dim firstNameBuilder As New StringBuilder()
    Dim middleNameBuilder As New StringBuilder()
    Dim lastNameBuilder As New StringBuilder()
    For Each n As String In names
        If firstNameBuilder.Length + n.Length <= 30 Then
            firstNameBuilder.Append(n)
            firstNameBuilder.Append(" ")
        ElseIf middleNameBuilder.Length + n.Length <= 30 Then
            middleNameBuilder.Append(n)
            middleNameBuilder.Append(" ")
        Else
            lastNameBuilder.Append(n)
            lastNameBuilder.Append(" ")
        End If
    Next
        Dim nameObj as new Name()
        nameObj.FirstName = firstNameBuilder.ToString()
        nameObj.MiddleName = middleNameBuilder.ToString()
        nameObj.LastName = LastNameBuilder.ToString()

    return nameObj
End Sub

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.