get country from the international phone number

Solutions on MaxInterview for get country from the international phone number by the best coders in the world

showing results for - "get country from the international phone number"
Jonah
15 Feb 2018
1/// <summary>
2/// Returns the Country based on an international dialing code.
3/// </summary>
4public static Country? GetCountry(ReadOnlySpan<char> phoneNumber) {
5  if (phoneNumber.Length==0) return null;
6
7  var isFirstDigit = true;
8  DigitInfo? digitInfo = null;
9  Country? country = null;
10  foreach (var digitChar in phoneNumber) {
11    var digitIndex = digitChar - '0';
12    if (isFirstDigit) {
13      isFirstDigit = false;
14      digitInfo = ByPhone[digitIndex];
15    } else {
16      if (digitInfo!.Digits is null) return country;
17
18      digitInfo = digitInfo.Digits[digitIndex];
19    }
20    if (digitInfo is null) return country;
21
22    country = digitInfo.Country??country;
23  }
24  return country;
25}
26