Nick
Nick

Reputation: 39

Splitting a full name string to separate variables (first, middle and last) without using the split function

So I was given the task to bifurcate a string with a full name and then print out the first and last name separately. For instance, input: Steve Robertson, output: First name: Steve Last name: Robertson.

I succeeded, it was fairly easy. But I'm having having trouble in dividing a full name string to first, last and middle. Here's what I've done so far.

Sub Main()
        Dim string1 As String = ""
        Dim string2 As String = ""
        Dim string3 As String = ""
        Dim string4 As String = ""
        Dim temp1 As String = ""
        Dim integer1 As Integer = 1
        Console.WriteLine("Enter the string you want to bifurcate: ")
        string1 = Console.ReadLine()
        While integer1 <> 0
            integer1 = InStr(string1, " ")
            Console.WriteLine(string1)
            string2 = Left(string1, integer1)
            Console.WriteLine(string2)
            string3 = Mid(string1, integer1 + 1)
            Console.WriteLine(string3)
            string4 = Mid(string1, integer1 + 1)
            Console.WriteLine(string4)
            string1 = string4
        End While
        Console.WriteLine("First name is: " & string2)
        Console.WriteLine("Second name is: " & string3)
        Console.WriteLine("Third name is: " & string4)
        Console.ReadKey()
    End Sub

Keep in mind that I'm only printing almost every single variable to see what their value is during the iteration. I can only use the len() function and whatever is already in the code.

EDIT:

So I fiddled around and finally got the thing, without the loop, but I was wondering if there was a cleaner/right way to do this without repeating the variables and also not needing to create any new ones either.

Sub Main()
    Dim string1 As String = ""
    Dim string2 As String = ""
    Dim string3 As String = ""
    Dim string4 As String = ""
    Dim integer1 As Integer
    Console.WriteLine("Enter the string you want to split: ")
    string1 = Console.ReadLine()
    integer1 = InStr(string1, " ")
    string2 = Left(string1, integer1)
    string3 = Mid(string1, integer1 + 1)
    integer1 = InStr(string3, " ")
    string4 = Left(string3, integer1)
    string3 = Mid(string3, integer1 + 1)
    Console.WriteLine("The first name is: " & string2)
    Console.WriteLine("The middle name is: " & string4)
    Console.WriteLine("The last name is: " & string3)
    Console.ReadKey()
End Sub

Upvotes: 0

Views: 3266

Answers (2)

Trevor
Trevor

Reputation: 8004

Here is one way to do it. Loop through the characters from the input of the user. Continue to do so concatenating them together and throw them into a List(Of String) that way the can be easily written out at the end... This account's for multiple spaces as well if there's more than one in between names. Also I put some comment's into the code so it can be easier to understand.

Note: This is only one way to do it... (there are other ways)

CODE TRIED AND TESTED

    Dim nList As New List(Of String)
    Dim uStr As String = String.Empty

    Console.WriteLine("Enter the string you want to bifurcate: ")
    uStr = Console.ReadLine()
    If Not String.IsNullOrEmpty(uStr) Then 'Make sure something was entered...
        Dim tStr As String = String.Empty

        'Loop through user's input...
        Do Until uStr = String.Empty
            For Each c As Char In uStr
                If Not Char.IsWhiteSpace(c) Then 'If it's a space we can add to the current string...
                    tStr &= c.ToString
                Else
                    'We can assume its another section of the name...
                    Exit For
                End If
            Next
            'If its a space, remove it from current string...
            If String.IsNullOrEmpty(tStr) Then uStr = uStr.Remove(0, tStr.Length + 1) : Continue Do
            'Add the string to the list, could be first name, middle or lastname?
            If nList.Count = 0 Then
                nList.Add("First Name: " & tStr)
            ElseIf nList.Count = 1 Then
                nList.Add("Middle Name: " & tStr)
            ElseIf nList.Count = 2 Then
                nList.Add("Last Name: " & tStr)
            End If

            'Now we can remove what we got from the users input...
            uStr = uStr.Remove(0, tStr.Length)
            tStr = String.Empty
        Loop

    End If

    'Finally write out the values...
    Console.WriteLine(String.Join(Environment.NewLine, nList.ToArray))
    Console.ReadLine()

Screenshot Of Program

enter image description here

Upvotes: 2

Jande
Jande

Reputation: 1705

Maybe something like this

 Dim fullName = "Juan Perez"
 Dim name = fullName.Substring(0, fullName.IndexOf(" "))
 Dim lastName = fullName.Substring(fullName.IndexOf(" ") + 1)

How to separate full name string

I would suggest that you design extension methods that will return First and Last Name

  Module Module1

 <Extension()>
 Public Function returnFirst(ByVal fullName As String) As String

    Return fullName.Substring(0, fullName.IndexOf(" "))
End Function

 <Extension()>

 Public Function returnLast(ByVal fullName As String) As String

    Return fullName.Substring(fullName.IndexOf(" ") + 1)

End Function

 End Module

'call it

'import module1

Dim Name as string = 'FirstName LastName'

MessageBox.Show(NAME.returnFirst)
MessageBox.Show(NAME.returnLast)

Upvotes: 0

Related Questions