0

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
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Nick
  • 39
  • 1
  • 8
  • You can only input the name once? Also if not how do you validate user input or does it matter? The current way is to much, this can be done in just a few lines... Also can you do anything you want, create a class, function etc...? – Trevor Apr 03 '16 at 18:29
  • I'm not sure I understand. Yes you can only input it once, which is supposed to be the whole name e.g. `Steve Earl Robertson`. – Nick Apr 03 '16 at 18:35
  • Awesome, can you create your own function, class etc? Also if user only enter's one string what about validation? – Trevor Apr 03 '16 at 18:36
  • Nope. It's supposed to be beginner tier code. – Nick Apr 03 '16 at 18:37
  • Beginner tier code, but they tell you that you can't use the `split` function... So if you can't create a function, class what are you supposed to use? Did they tell you that? – Trevor Apr 03 '16 at 18:38
  • You can use the length function and whatever is already in the code. Not classes and stuff. I should've clarified, the code in the original question. I know I can get the result from just solving the mumbled puzzle in the program. But I don't know what exactly is needed to be fiddled with. – Nick Apr 03 '16 at 18:42
  • `length function and whatever is already in the code` what code, the framework functions? – Trevor Apr 03 '16 at 18:43

2 Answers2

2

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

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • I really appreciate you answering this. I have one question though, is a 'list' of a string basically an array? – Nick Apr 03 '16 at 23:28
0

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)
Community
  • 1
  • 1
Jande
  • 1,695
  • 2
  • 20
  • 32
  • 1
    @Nick take a look at https://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx – Jande Apr 03 '16 at 18:48