I'm trying to make a program that lets user to input an array of lastnames then firstnames. Then display the a list of firstname first, then the lastname. For example:

Input: Smith, John
: Doe, Jane
displays like:
John Smith
Jane Doe


This is what i got so far.

Module Ex04

    Sub Main()
        Dim m As Integer
        Dim anArrayList As ArrayList = New ArrayList(5)
        Dim inputString As String = "!"
        Do While Not inputString.StartsWith("$")
            Console.WriteLine("Enter a full name, Last, First I. - $ to end input")
            inputString = Console.ReadLine()
            anArrayList.Add(inputString)



        Loop
        For m = 0 To 5
            Console.WriteLine(anArrayList(m))
            m = m + 1

        Next
        Console.ReadLine()
    End Sub

End Module

May be you are looking for,

Dim s = "aa,bb,cc,dd"
Dim t = System.Text.RegularExpressions.Regex.Replace(s, "(\w+),(\w+)", "$2,$1")

Edited 6 Years Ago by __avd: n/a

nope, I'm looking for a split method can that reverse the order of the names. Smith, John will become John Smith

For m = 0 To 5
Dim currentName() as string=anArrayList(m).split(","c)
            Console.WriteLine(String.Format("{0} {1}",currentName(1).trim,currentName(0).trim))
            m = m + 1
        Next

But you should ofc check if the input is correct before starting the loops

Hi,

Here is a different example using an String.empty to stop input, and uses the number of elements in the arraylist for the loop count....

Sub Main()

        Dim nameAL As ArrayList = New ArrayList

        'Write
        Console.WriteLine("Enter a full name, Last, First")

        Dim emptyLine As Boolean = False
        Dim nameString As String


        Do Until emptyLine = True
            nameString = Console.ReadLine

            If nameString = String.Empty Then
                emptyLine = True
            Else
                nameAL.Add(nameString)
            End If

        Loop


        'Display Names

        For i As Integer = 0 To nameAL.Count - 1
            Dim alSplit() As String
            alSplit = nameAL(i).ToString.Replace(" ", "").Split(CChar(","))
            Console.WriteLine(alSplit(1) & " " & alSplit(0))

        Next
      

        'Pause
        Console.ReadLine()


    End Sub
This article has been dead for over six months. Start a new discussion instead.