How to get state abbreviations quickly with a small amount of code

On the MSDN forums, I suspect the person in this post is attempting to get a state abbreviation.

If I were trying to get a state abbreviation, I would do it like this:

Option Strict On
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(GetStateAbbrevation(TextBox1.Text))
    End Sub
    Public Function GetStateAbbrevation(ByVal stateName As String) As Object
        Dim States As New List(Of String) From {"ALabama", "AlasKa", "AriZona", "ARkansas", "CAlifornia", "COlorado", _
                                                "ConnecTicut", "DElaware", "FLorida", "GeorgiA", "HawaiI", _
                                                "IDaho", "ILlinois", "INdiana", "IowA", "KansaS", "KentuckY", _
                                                "LousianaA", "MainE", "MarylandD", "MAssachusetts", "MIchigan", _
                                                "MiNnesota", "MiSsissippi", "MissOuri", "MonTana", "NEbraska", _
                                                "NeVada", "NewHampshire", "NewJersey", "NewMexico", "NewYork", _
                                                "NorthCarolina", "NorthDakota", "OHio", "OKlahoma", "ORegon", _
                                                "PennsylvAnia", "RhodeIsland", "SouthCarolina", "SouthDakota", _
                                                "TenNessee", "TeXas", "UTah", "VermonT", "VirginiA", "WAshington", _
                                                "WestVirginia", "WIconsin", "WYoming"}
        stateName = stateName.Replace(" ", "").ToLower
        Dim result As String = String.Empty
        For Each state As String In States
            If state.ToLower = stateName.ToLower Then
                For Each c As Char In state
                    If Char.IsUpper(c) Then result = result & c
                Next
            End If
        Next
        Return result
    End Function
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s