Get Initials from a String in VB.NET

So I've been in way over my head lately, converting a massive spaghetti-code Classic ASP application to .NET 2.0. Recently my client supplied with one of those Intro-to-Programming exam questions that was kinda fun, so I thought I'd post the answer here.

He needed to pull a person's initials out of a string, and he needed to handle either "LastName, FirstName" or "FirstName LastName".

Now, you can't just strip out all the capital letters, because Scottish guys like me will throw a wrench in that logic. So the easies way to do it is to split the string with a space character, and grab the first letter from each resulting string.

So here is the result, with a few test cases called from a test webpage for good measure. Hope you like:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.Write(GetInitialsFromString("Robert McLaws") & "<br />")
        Response.Write(GetInitialsFromString("Bill Gates") & "<br />")
        Response.Write(GetInitialsFromString("Robert W. McLaws") & "<br />")
        Response.Write(GetInitialsFromString("McLaws, Robert") & "<br />")
        Response.Write(GetInitialsFromString("McLaws, Robert W.") & "<br />")
    End Sub

    Public Function GetInitialsFromString(ByVal fullName As String) As String
        If fullName.Contains(",") Then
            fullName = NormalizeName(fullName)
        End If
        Dim nameArray As String() = fullName.Split(" ")
        Dim initials As String = String.Empty
        For Each name As String In nameArray
            initials += name.Chars(0)
        Next
        Return initials.ToUpper()
    End Function

    Public Function NormalizeName(ByVal fullName As String) As String
        Dim name As String() = fullName.Split(",")
        Return String.Format("{0} {1}", Trim(name(1)), Trim(name(0)))
    End Function

So if anyone ever needs to do this... there you go :). It's also a useful function for person-related .NET objects, so you can grab the initials by calling PersonObject.Initials instead of having to do it manually.

2 Comments

  • You could also use Regex to make really quick work of this:

    Imports System.Text.RegularExpressions

    Module Module1

    Function GetInitials(ByRef fullName As String) As String
    Return Regex.Replace(fullName, "^(?'b'\w)\w*,\s*(?'a'\w)\w*$|^(?'a'\w)\w*\s*(?'b'\w)\w*$", "$2$1", RegexOptions.Singleline)
    End Function

    Sub Main()
    Console.WriteLine(GetInitials("David Findley"))
    Console.WriteLine(GetInitials("Findley, David"))
    End Sub

    End Module

  • actually just noticed I forgot to change the replace string. Should be this:

    Return Regex.Replace(fullName, "^(?'b'\w)\w*,\s*(?'a'\w)\w*$|^(?'a'\w)\w*\s*(?'b'\w)\w*$", "${a}${b}", RegexOptions.Singleline)

    The basic gist is this:

    ^ matches start of string
    (?'b'\w) captures 1st character of a word and sores it in a group called 'b'
    \w* matches 0 or more characters (rest of the name)
    , matches a comma
    \s* matches 0 or more spaces
    (?'a'\w)\w* matches the 2nd part of the name cpaturing the first letter into a group named 'b'
    $ matches end of string
    | or the alternate pattern w ithout the ,
    Notice the 'a' and 'b' are swapped

    Regex is def more complex but you can do alot of stuff in a small amount of code.

Comments have been disabled for this content.