Robert McLaws: FunWithCoding.NET

Public Shared Function BrainDump(ByVal dotNet As String) As [Value]

News

<script type="text/javascript"><!-- google_ad_client = "pub-4330602465258980"; google_hints = "ASP.NET, VB.NET, C#, C#.NET, WindowsForms, .NET Framework, VS2005, Visual Studio, XAML, WinFX, Windows Workflow, WPF, WCF, Atlas, NetFX3, Visual Studio Orcas"; google_ad_width = 120; google_ad_height = 240; google_ad_format = "120x240_as"; google_ad_type = "text_image"; google_ad_channel ="4997399242"; google_color_border = "B6C9E7"; google_color_bg = "EFEFEF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "002C99"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<!--
-->

You should feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever. That said, I will most likely only delete abusive, profane, rude, or annonymous comments, so keep it polite, please.

Blogroll

Cool .NET Articles

My .NET Tools

My Builder.com Articles

My MSKB Articles

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.

Comments

findleyd said:

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

# October 17, 2006 1:14 AM

findleyd said:

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.

# October 17, 2006 1:23 AM