String Replace c# for address abbreviation

Viewed 368

I have a code for conversion but some address have different result to what is expected.

23 Starling St => 3 Streetarling Street which is wrong and it should be 23 Starling Street

1 St Johns Ct => 1 Street Johns Ct => Correct

This is the code:

private string StreetConversion(string address, Order order)
{
    string[] addressList = address.Split(' ');

    foreach (string add in addressList) 
    {
        if(add == "pde")
            address = address.Replace("pde", "Parade");
        if (add == "Pde")
            address = address.Replace("Pde", "Parade");
        if (add == "Rd")
            address = address.Replace("Rd", "Road");
        if (add == "rd")
            address = address.Replace("rd", "Road");
        if (add == "St")
            address = address.Replace("St", "Street");
        if (add == "st")
            address = address.Replace("st", "Street");
    }

    order.ShipAddress1 = address;
    return address;
}
4 Answers

You need to replace given word instead of replacing all occurences of that word in address variable,

private string StreetConversion(string address, Order order)
{
    string[] addressList = address.Split(' ');
    StringBuilder newAddress = new StringBuilder();

    foreach (string add in addressList) 
    {
        if(add.ToLower() == "pde")
            newAddress.Append("Parade ");
        else if (add.ToLower() == "rd")
            newAddress.Append("Road ");
        else if (add.ToLower() == "st")
            newAddress.Append("Street ");
        else
            newAddress.Append(add+ " ");
    }

    order.ShipAddress1 = newAddress.ToString();
    return newAddress.ToString();
}

First of all, let's extract model: acronyms and their substitutions

  private static Dictionary<string, string> acronyms =
    new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
      {"pde", "Parade"},
      {"rd", "Road"},
      {"st", "Street"}, 
      //TODO: Add more pairs if required, say, {"sq", "square"},
    };

Then we can easily transform the string address:

  using System.Linq;

  ...

  private string StreetConversion(string address, Order order) {
    string result = string.Join(" ", address
      .Split(' ')
      .Select(word => acronyms.TryGetValue(word, out var newWord) ? newWord : word));

    order.ShipAddress1 = result;
    return result;  
  }

Step 1: Lets follow "The Single Responsibility Principle" to keep code cleaner and maintainable.

"The Single Responsibility Principle: A class or method should have only a single responsibility."

So you need to take out the following code from the method:

order.ShipAddress1 = address;

Step 2: Your method responsibility should only be converting the street and return it back, and you can reach that in many ways, the following is one of the ways:

private string StreetConversion(string address) {
    var newAddress= string.Empty;
    foreach (var s in address.Split(' ')) {
        newAddress+= (s.ToLower()) switch {
                "st" => "Street",
                "rd" => "Road",
                "pde" => "Parade",
                _ => s + " ",
        };
    }
    return newAddress;
}

Step 3: Calling the method

order.ShipAddress1 = StreetConversion(OldAddress);

You can use Regex also to replace the work with another word.

s = Regex.Replace(s, @"\boldVal1\b", "newVal1", RegexOptions.IgnoreCase);
s = Regex.Replace(s, @"\boldVal2\b", "newVal2", RegexOptions.IgnoreCase);
Related