Asynchronous
Asynchronous

Reputation: 3977

How to Split Street Address String and Replace Individual elements

I need to take a street address, just the address line, 111 Stackoverflow Drive North split the individual strings and replace specific elements.

For example: I need to replace elements in the string so that it looks like this:

Input: 111 Stackoverflow Drive North Output: 111 Stackoverflow Dr N So basically, if the address line contains Av or Avenue then replace with Ave.

The only way I know how to do this is to use the Replace Method back to back.

Example:

 string input = "111 Stackoverflow Drive North";
 string address = input.ToLower().Replace("north", "N").Replace("drive", "Dr");

This looks pretty verbose: I was thinking about creating a list of values then split the address and replace but not sure how to put the two together.

Upvotes: 0

Views: 664

Answers (5)

GEEF
GEEF

Reputation: 1185

You are on the right track with the replace, but I would recommend, as others have, a dictionary with look ups and replacements inside of a loop. See below:

Dictionary<string, string> replacementDictionary = new Dictionary<string, string>();
replacementDictionary.Add("north", "N");
replacementDictionary.Add("south", "S");
// Add more entries here

Then when you have an address with replacement in question, you can search your dictionary for a replacement entry:

string addressInQuestion = "101 north Almond drive";
string finalAddress;
foreach(string token in addressInQuestion.Split(' '))
{
    string replacementToken = token;
    replacementDictionary.TryGetValue(token, out replacementToken)
    finalAddress += replacementToken;
}

This will also avoid replacing possible unintended replacements of 'combined-token' words such as "101 Northbridge Way". Of course, you will have to write your capitalization and spacing rules yourself :)

Upvotes: 0

enorl76
enorl76

Reputation: 2655

You'd want to use this codeplex project that tokenizes and parses address:

http://usaddress.codeplex.com/

Then you have access to the various pieces of information.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

You could use StringBuilder.Replace in a loop, for example with this method:

public static String ReplaceAll(string original, Dictionary<String, String> replacements)
{
    var sb = new StringBuilder(original);
    foreach (var kv in replacements)
    {
        sb.Replace(kv.Key, kv.Value);
    }
    return sb.ToString();
}

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

Address normalization is a VERY tricky subject. If you need to be USPS conform, don't try to write it yourself. There is CASS certified software that does this perfectly (expensive), and a number of low-cost solutions that do it well.

Having said that, you can tokenize the input string, and then concatenate the values together either by using the replacement if one is defined, otherwise by using the original value.

Dictionary<string, string> replacements = new Dictionary<string, string>()
{
    { "DRIVE", "DR" }
};

string[] tokens = originalAddress.Split(new char[] { ' ', '\t' });
StringBuilder normalized = new StringBuilder();

foreach (string t in tokens)
{
    string rep;
    bool found = replacements.TryGetValue(t.ToUpper(), out rep);

    if (found)
    {
        normalized.Append(rep);
    }
    else
    {
        normalized.Append(t);
    }
    normalized.Append(' ');
}

// normalized.ToString() contains the normalized address

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

I would use a Dictionary for this

var dict = new Dictionary<string, string> {{"north", "N"}, {"drive", "Dr"}};

string address = string.Join(" ",
            input.Split().Select(x =>
            {
                if (dict.ContainsKey(x.ToLower())) 
                     return dict[x.ToLower()];

                return x;
            }));

Upvotes: 0

Related Questions