/// <summary> /// Converts from Name to requested format /// </summary> /// <param name="input"></param> /// <param name="countryCodeFormat"></param> /// <returns></returns> private static string ConvertFromName(string input, CountryCodeFormat countryCodeFormat) { if (countryCodeFormat == CountryCodeFormat.Alpha2 && CountryData.Alpha2CodeByCountry.ContainsKey(input)) { return(CountryData.Alpha2CodeByCountry[input]); } else if (countryCodeFormat == CountryCodeFormat.Alpha3 && CountryData.Alpha3CodeByCountry.ContainsKey(input)) { return(CountryData.Alpha3CodeByCountry[input]); } else if (countryCodeFormat == CountryCodeFormat.Numeric && CountryData.NumericCodeByCountry.ContainsKey(input)) { return(CountryData.NumericCodeByCountry[input]); } else if (countryCodeFormat == CountryCodeFormat.Name) { return(input); } return(null); }
/// <summary> /// Converts from Numeric to requested format /// </summary> /// <param name="input"></param> /// <param name="countryCodeFormat"></param> /// <returns></returns> private static string ConvertFromNumeric(string input, CountryCodeFormat countryCodeFormat) { if (countryCodeFormat == CountryCodeFormat.Alpha2 && CountryData.Alpha2CountryCodeByNumeric.ContainsKey(input)) { return(CountryData.Alpha2CountryCodeByNumeric[input]); } else if (countryCodeFormat == CountryCodeFormat.Alpha3 && CountryData.Alpha3CountryCodeByNumeric.ContainsKey(input)) { return(CountryData.Alpha3CountryCodeByNumeric[input]); } else if (countryCodeFormat == CountryCodeFormat.Numeric) { return(input); } else if (countryCodeFormat == CountryCodeFormat.Name && CountryData.CountryByNumericCode.ContainsKey(input)) { return(CountryData.CountryByNumericCode[input]); } return(""); }
/// <summary> /// Returns Country Code /// </summary> /// <param name="country"></param> /// <param name="countryCodeFormat">Alpha2 is the default</param> /// <returns></returns> public static string GetCountryCodeByCountry(string country, CountryCodeFormat format = CountryCodeFormat.Alpha2) { CountryData CountryData = new CountryData(); string output = ""; if (country == null) { return(null); } if (IsCountryName(country)) { output = ConvertFromName(country, format); } else if (IsAlpha2(country)) { output = ConvertFromAlpha2(country, format); } else if (IsAlpha3(country)) { output = ConvertFromAlpha3(country, format); } else if (IsNumeric(country)) { output = ConvertFromNumeric(country, format); } if (!string.IsNullOrEmpty(output)) { return(output); } else { // it's not a country match or a countryCode match so let's get fuzzy return(FuzzyByFormat(format, country)); } }
private static string FuzzyByFormat(CountryCodeFormat format, string country) { string fuzzyCountryMatch = ""; string output = ""; if (format == CountryCodeFormat.Alpha2) { fuzzyCountryMatch = FuzzyMatch(CountryData.Alpha2CodeByCountry, country, significantCountryMatch); } else if (format == CountryCodeFormat.Alpha3) { fuzzyCountryMatch = FuzzyMatch(CountryData.Alpha3CodeByCountry, country, significantCountryMatch); } else if (format == CountryCodeFormat.Numeric) { fuzzyCountryMatch = FuzzyMatch(CountryData.NumericCodeByCountry, country, significantCountryMatch); } if (fuzzyCountryMatch != null) { return(fuzzyCountryMatch); } else { // assume if it's > 3 it's not a code and do not do fuzzy code matching if (country.Length > 3) { return(null); } // 3 or less, let's fuzzy match string fuzzyCodeMatch = ""; if (format == CountryCodeFormat.Alpha2) { fuzzyCodeMatch = FuzzyMatch(CountryData.CountryByAlpha2Code, country, significantCodeMatch); if (fuzzyCodeMatch != null) { output = CountryData.Alpha2CodeByCountry[fuzzyCodeMatch]; } } else if (format == CountryCodeFormat.Alpha3) { fuzzyCodeMatch = FuzzyMatch(CountryData.CountryByAlpha3Code, country, significantCodeMatch); if (fuzzyCodeMatch != null) { output = CountryData.Alpha3CodeByCountry[fuzzyCodeMatch]; } } else if (format == CountryCodeFormat.Numeric) { fuzzyCodeMatch = FuzzyMatch(CountryData.CountryByNumericCode, country, significantCodeMatch); if (fuzzyCodeMatch != null) { output = CountryData.NumericCodeByCountry[fuzzyCodeMatch]; } } if (output != null) { return(output); } } return(null); }