We've got a legacy custom address control. It provides a free text form into which users can enter any address, even a partial or invalid address; see green arrow in the screenshot:
Entering an address in this free text form is to provide better user experience; however, the address has to be structured for further processing. Consequently, the address is analysed to determine street, town, post code, country etc.
To determine the country seems fairly easy. Our current (for readability simplified) source code looks like this:
private static string DetermineCountryFromAddress(string fullAddress)
{
// determine list of countries found in the full address
string[] addressLines = fullAddress.Split(Environment.NewLine.ToCharArray());
IList<string> countries = new List<string>();
foreach (string addressLine in addressLines)
{
// check whether there's a country name hidden in this address line
string countryName;
if (ContainsCountry(addressLine, out countryName))
countries.Add(countryName);
}
// if there has been a country found, return the country found last;
// otherwise, return the default country (constant)
return countries.Any() ? countries[countries.Count - 1] : DefaultCountryName;
}
For curiosity, this is how our simplified ContainsCountry()
method looks like:
private static bool ContainsCountry(string addressLine, out string foundCountryName)
{
// check against all countries
foreach (string countryName in
AllCountryNames.Where(countryName => addressLine.Contains(countryName)))
{
foundCountryName = countryName;
return true;
}
// nothing found
foundCountryName = null;
return false;
}
This solution though doesn't address these requirements:
- Country can be at any line, not only the last one
- If no country provided, country names which form part of street names should be ignored
Is there anybody who has a smart enhancement (solution) that fully addresses one or both requirements? Using an external service provider for address validation is excluded from acceptable answers.