ASP.NET / VB: Replace US State Abbreviations with Names

Posted on 2012-09-16
Last Modified: 2012-09-17
How can I replace state abbreviations with state names using ASP.NET and VB?

<%@ Page Language="VB" AutoEventWireup="false" %>

Dim array(,) As Object = { _
     {"ak","Alaska"}, _
     {"al","Alabama"}, _
     {"ca","California"} _

Dim Xyz As String = "Lakes in ak are cold cold but it is always hot in AL and can be hot or cold in ca"

' Should say:
' Lakes in Alaska are cold cold but it is always hot in Alabama and can be hot or cold in California


Open in new window

Question by:hankknight
    LVL 83

    Accepted Solution

    I would change array to Dictionary(of string, string) and then loop through it doing a Replace on the string.

    Dim states As New Dictionary(of string, string)
    states.add("AK", "Alaska")
    states.add("AL", "Albama")
    states.add("CA", "California")

    Dim Xyz As String = "Lakes in ak are cold cold but it is always hot in AL and can be hot or cold in ca"

    For Each (state as string in states.Keys)
      xyz = xyz.Replace(" " & state & " ", " " & states(state) & " ")
    LVL 16

    Author Comment

    Thanks but I get an error.

    I posted a related question here:

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now