Programming Fundamentals/Strings/VB.NET

From Wikiversity
Jump to navigation Jump to search

strings.vb[edit]

' This program splits a given comma-separated name into first and last name
' components and then displays the name.
'
' References:
'     https://www.mathsisfun.com/temperature-conversion.html
'     https://en.wikibooks.org/wiki/Visual_Basic_.NET

Imports System

Public Module Strings
    Sub Main
        Dim Name As String
        Dim First As String
        Dim Last As String

        Name = GetName()
        Last = GetLast(Name)
        First = GetFirst(Name)
        DisplayName(First, Last)
    End Sub

    Function GetName() As String
        Dim Name As String
        Dim Index As Integer

        Do
            Console.WriteLine("Enter name (last, first):")
            Name = Console.ReadLine()
            Index = Name.IndexOf(",")
        Loop While (Index < 0)
    
        Return Name
    End Function

    Function GetLast(Name As String) As String
        Dim Last As String
        Dim Index As Integer
    
        Index = Name.IndexOf(",")
        If(Index < 0)
            Last = ""
        Else
            Last = Name.Substring(0, Index)
        End If

        Return Last
    End Function

    Function GetFirst(Name As String) As String
        Dim First As String
        Dim Index As Integer
    
        Index = name.IndexOf(",")
        If(Index < 0)
            First = ""
        Else
            First = Name.Substring(Index + 1, Name.Length - Index - 1)
            First = First.Trim()
        End If
    
        Return First
    End Function

    Sub DisplayName(First As string, Last As string)
        Console.WriteLine("Hello " + first + " " + last + "!")
    End Sub
End Module

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own VB.NET compiler / interpreter / IDE.

See Also[edit]